首页 > 代码库 > XDOJ_1077_循环节长度
XDOJ_1077_循环节长度
http://acm.xidian.edu.cn/problem.php?id=1077
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<cmath> #define LL long long using namespace std; LL PowerMod(LL a, LL b, LL c) { LL ans = 1; a = a % c; while(b) { if(b&1) ans = (ans*a)%c; b = b>>1; a = (a*a)%c; } return ans; } int euler(int n) { int res = n,a = n; for(int i = 2;i*i <= a;i++) { if(a%i == 0) { res = res/i*(i-1); while(a%i == 0) a /= i; } } if(a > 1) res = res/a*(a-1); return res; } int gcd(int a,int b) { return b?gcd(b,a%b):a; } int main() { int p,q; while(~scanf("%d%d",&p,&q)) { q /= gcd(p,q); while(q%2 == 0) q /= 2; while(q%5 == 0) q /= 5; if(q == 1) { printf("0\n"); continue; } int x = euler(q),now = 1; int xx = sqrt(x),flag = 0; for(int i = 1;i <= xx;i++) { if(x%i) continue; if(PowerMod(10,i,q) == 1) { printf("%d\n",i); flag = 1; break; } } if(flag) continue; for(int i = xx;i >= 1;i--) { if(x%i) continue; if(PowerMod(10,x/i,q) == 1) { printf("%d\n",x/i); break; } } } return 0; }
XDOJ_1077_循环节长度
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。