欧拉定理 hdu 1395

2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13749    Accepted Submission(s): 4239


Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
 

 

Input
One positive integer on each line, the value of n.
 

 

Output
If the minimum x exists, print a line with 2^x mod n = 1.

Print 2^? mod n = 1 otherwise.

You should replace x and n with specific numbers.
 

 

Sample Input
2
5
 

 

Sample Output
2^? mod 2 = 1
2^4 mod 5 = 1
 
题意给你一个数m要你求一个数x使得2^x对于m取模使得等式为1否则输出?
可以暴力不过最好使用欧拉定理,后面附上维基百科的资料写的慢详细的:
http://zh.wikipedia.org/wiki/欧拉定理_(数论)
http://zh.wikipedia.org/wiki/欧拉函数
欧拉定理里比较重要的是欧拉函数例如一个数为x = 9,欧拉函数(x)= 6,因为小于9并且与他互质的数有1 2 4 5 7 8这6个所以欧拉函数(9)=6。求欧拉函数有个公式在维基百科里有就自己慢慢看吧
/****************************************
c语言我不知道,c++函数里有euler_phi()也就是欧拉函数
代码有点丑不过这就是欧拉函数的求法
****************************************/
LL Euler_phi(LL n)
{
    LL m = sqrt(n + 0.5),ans = n;
    for(int i = 2; i <= m; i++)
    {
        if(n % i == 0)
        {
            ans = ans/i*(i-1);
            while(n % i == 0) n /= i;
        }
    }
    if(n > 1)  ans = ans/n*(n-1);
    return ans;
}

当然欧拉函数是解,不过并不是最小解

最小的解在与x互质的数和欧拉函数的解之间

所以找出欧拉函数之后找出与x互质的数之后暴力搜索就好了

posted @ 2015-04-25 21:22  乱齐八糟  阅读(314)  评论(0)    收藏  举报