简单数论问题
2014-07-20 19:56
204 查看
Problem Description Given two positive integers a and N, satisfaing gcd(a,N)=1, please find the smallest positive integer x with a^x≡1(mod N). Input First is an integer T, indicating the number of test cases. T<3001. Each of following T lines contains two positive integer a and N, separated by a space. a<N<=1000000. Output For each test case print one line containing the value of x. Sample Input 2 2 3 3 5 Sample Output 2 4 //题意:求最小的x使 a^x % n = 1.(注意:用欧拉定理求的x不一定是最小的,有的还需要求要去除因子.) //标程: #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; const int Max = 1000010; int prime[Max], phi[Max]; void euler() //求欧拉函数值; { prime[0] = prime[1] = 0; for(int i = 2;i <= Max; i ++) prime[i]=1; for(int i = 2; i*i <= Max; i ++) if(prime[i]) for(int j=i*i;j<=Max;j+=i) prime[j]=0; for(int i=1;i<=Max;i++) phi[i]=i; for(int i=2;i<=Max;i++) if(prime[i]) for(int j = i; j <= Max; j += i) phi[j] = phi[j]/i * (i-1); } int Mod(int a, int b, int c) { int ans = 1; long long aa = a; while(b) { if (b % 2) ans = ans * aa % c; aa = aa * aa % c; b /= 2; } return ans; } int main() { // freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); euler(); int t, a, n; cin >> t; while(t --) { cin >> a >> n; int sn = (int)sqrt(phi ), ans = n; for(int i = 1; i <= sn; i++) { if (phi % i == 0) { if (Mod(a, i, n) == 1 && ans > i) ans = i; if (Mod(a, phi / i, n) == 1 && ans > phi / i) ans = phi / i; } } cout << ans << endl; } return 0; }
相关文章推荐
- (step7.2.3)hdu 2554(N对数的排列问题——简单数论)
- 简单数论问题
- 一个简单的数论问题
- 街区最短路径问题-简单数论
- (step7.2.3)hdu 2554(N对数的排列问题——简单数论)
- UVA-575-Skew Binary (简单数论问题)
- 简单数论问题
- poj 2992 Divisors 简单的数论问题
- POJ 1845 Sumdiv 简单数论问题
- 简单但郁闷的问题
- JAVA学习中应该注意的一些简单问题?
- JAVA学习中应该注意的一些简单问题?
- 难得今天积极的帮别人回答问题,顺便贴出来。这可能是学习事件的最简单的例子了
- 学习.NET遇到的第一个问题。原来解决这么简单
- 学习ejb并配置一个简单的helloEjb是遇到问题后总结的经验。
- Iframe自适应页面高度问题的最简单最适用的解决办法
- 简单基础的问题,但是非常容易出错.
- struts 中文问题解决(简单实用)
- 简单的分页控件问题
- 简单问题综合