zoukankan      html  css  js  c++  java
  • 省赛补题 k题数论Happy Equation

    传送门:
    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4123

    Little Sub has just received an equation, which is shown below, as his birthday gift.Given the value of , please help Little Sub count the number of  () which satisfies the equation.

    Input

    There are multiple test cases. The first line of the input contains an integer  (about 1000), indicating the number of test cases. For each test case:

    The first and only line contains two integers  and  (, ).

    Output

    For each test case output one line containing one integer, indicating the answer.

    Sample Input

    2
    6 12
    8 16
    

    Sample Output

    1023 16383
    
    • 先打表找一下规律
    #include <bits/stdc++.h>
    #define REP(i, a, b) for(int i = a; i < b; i++)
    #define REP_(i, a, b) for(int i = a; i <= b; i++)
    #define sl(n) scanf("%lld", &n);
    #define si(n) scanf("%d", &n);
    #define RepAll(a) for(auto x: a)
    #define cout(ans) cout << ans << endl;
    typedef long long ll;
    ll two[] = {0,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
    
    using namespace std;
    ll qpow(ll a, ll n)//计算a^n % mod
    {
        ll re = 1;
        while(n)
        {
            if(n & 1)//判断n的最后一位是否为1
                re = (re * a);
            n >>= 1;//舍去n的最后一位
            a = (a * a) ;//将a平方
            //cout << re << '
    ';
        }
    
        return re;
    }
    ll qpow(ll a, ll n,ll mod)//计算a^n % mod
    {
        ll re = 1;
        while(n)
        {
            if(n & 1)//判断n的最后一位是否为1
                re = (re * a) % mod;
            n >>= 1;//舍去n的最后一位
            a = (a * a) % mod;//将a平方
        }
        return re % mod;
    }
    int main()
    {
        ll cnt = 0;
        for(ll p = 1; p <= 30; p++){
            for(ll a = 1; a <= 50; a++){
                cnt = 0;
                for(ll x = 1; x < 1000; x++){
                    if(qpow(a, x) == qpow(x, a)){
                        cnt++;
                    }
                }
                cout << cnt << '
    ' << "----------------------" << '
    ';
            }
    
        }
    }
    大概可以的到这样一张表

    1> 那么可以得到这么一个规律就是当a 为奇数的时候, 其答案只可能是 1, 就是满足答案的x只有一个

    注意求解的是满足的x的个数,而不是x的大小

    2>

    #include <bits/stdc++.h>
    #define REP(i, a, b) for(int i = a; i < b; i++)
    #define REP_(i, a, b) for(int i = a; i <= b; i++)
    #define sl(n) scanf("%lld", &n);
    #define si(n) scanf("%d", &n);
    #define RepAll(a) for(auto x: a)
    #define REP_two(i, a, b) for(int i = a; i <= b; i+=2)
    #define cout(ans) cout << ans << endl;
    typedef long long ll;
    ll two[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
    ll qpow(ll a, ll n)//计算a^n % mod
    {
        ll re = 1;
        while(n)
        {
            if(n & 1)//判断n的最后一位是否为1
                re = (re * a);
            n >>= 1;//舍去n的最后一位
            a = (a * a) ;//将a平方
        }
        return re;
    }
    ll qpow(ll a, ll n,ll mod)//计算a^n % mod
    {
        ll re = 1;
        while(n)
        {
            if(n & 1)//判断n的最后一位是否为1
                re = (re * a) % mod;
            n >>= 1;//舍去n的最后一位
            a = (a * a) % mod;//将a平方
        }
        return re % mod;
    }
    using namespace std;
    int main()
    {
        ll t;
        cin >> t;
        REP_(o, 1, t){
            ll a, p;
            sl(a);sl(p);
            if(a % 2 == 1){
                printf("1
    ");
                continue;
            }
            //小范围暴力
            ll ans = 0;
            REP_two(i, 2, p){
                //qpow(a, i) == qpow(i, a, mod)
                if(qpow(a, i) == qpow(i, a)){
                    ans++;
                }
            }
            ll tmp = p / a;
            if((p) % a){
                tmp++;
            }
            //cout << ans << endl;
            
            cout << cout <<  ans + (two[p] / two[tmp] - p / two[tmp]) << '
    ' << '
    ';
        }
        return 0;
    }
    

    当x大于等于p时   a ^ x % 2 ^ p 必为 0   此时只要使 x ^ a % 2 ^ p 也为 0,  那么当x ^ a 为 2 ^ p的倍数时右边的取余才为0 , 也就是看2 ^ p是2 ^ (p / a)的多少倍即可。

  • 相关阅读:
    错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法
    C语言习题
    嵌入式芯片STM32F407
    c语言课后习题
    求方程式的根
    C语言课后习题
    LINUX常用指令
    在 pythonanywhere 上搭建 django 程序(Virtualenv+python2.7+django1.8)
    Git远程操作详解
    ./configure,make,make install的作用
  • 原文地址:https://www.cnblogs.com/ygbrsf/p/12583010.html
Copyright © 2011-2022 走看看