标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 879 Accepted Submission(s): 421
#include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<string> #include<iostream> #include<queue> #include<stack> #include<map> #include<vector> #include<set> using namespace std; typedef long long LL; #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R #pragma comment(linker, "/STACK:102400000,102400000") const int maxn = 1e5 + 300; const LL INF = 0x3f3f3f3f; typedef long long LL; typedef unsigned long long ULL; LL vis[maxn], a[maxn]; int main(){ LL x, m, k, c; int T, cas = 0; scanf("%d",&T); while(T--){ scanf("%lld%lld%lld%lld",&x,&m,&k,&c); memset(vis,0,sizeof(vis)); int nn = 0, le = 0, st = 1; LL cc; LL n = 0; for(int i = 1; ; i++){ n = n*10; n = n + x; n = n%k; if(vis[n]){ cc = n; break; }else{ vis[n] = 1; nn++; a[nn] = n; } } for(int i = 1; i <= nn; i++){ if(a[i] == cc){ le = nn+1 - i; st = i; break; } } int flag = 0; if(m < st){ if(a[m] == c){ flag = 1; } }else{ m -= st; m %= le; if(a[st+m] == c){ flag = 1; } } printf("Case #%d:\n",++cas); if(flag) puts("Yes"); else puts("No"); } return 0; } /* 55 3 5 99 69 3 4 4 2 3 8 4 2 2 8 3 2 */
解题思路:数学方法。
转自http://m.blog.csdn.net/article/details?id=51471639
这个数要mod k ,那这个数应该怎么表示呢?
就这样转化了,然后10^m可以通过快速幂解决,但是很明显,除以9操作怎么办,除法取模,余数是会改变的,逆元?但是9和k不一定互质,且k也不一定是质数,所以扩展欧几里得和费马小定理都不能用了,束手无策
好吧,这里提供一种小方法
就这样经过几步转化,我求d不需要进行除法取模了,那我上面的问题不就解决了?对的。
#include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<string> #include<iostream> #include<queue> #include<stack> #include<map> #include<vector> #include<set> using namespace std; typedef long long LL; #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R #pragma comment(linker, "/STACK:102400000,102400000") const int maxn = 1e5 + 300; const LL INF = 0x3f3f3f3f; typedef long long LL; typedef unsigned long long ULL; LL quick(LL x,LL n,LL p){ if(n == 0) return 1; LL ret = 1; while(n){ if(n&1) ret = ret*x % p; n = n >> 1; x = x*x % p; } return ret; } int main(){ LL x, m, k, c; int T, cas = 0; scanf("%d",&T); while(T--){ scanf("%lld%lld%lld%lld",&x,&m,&k,&c); k*=9; LL ans = quick(10,m,k); ans = (ans - 1 + k) % k; ans /= 9; k /= 9; ans = ans * x % k; bool flag = 0; if(ans == c) flag = 1; printf("Case #%d:\n",++cas); if(flag) puts("Yes"); else puts("No"); } return 0; }
HDU 5690——All X——————【快速幂 | 循环节】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/5521041.html