1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 #defineLLD I64d5 #ifdef _WIN326 #defineLLD "%i64d"7 #else8 #defineLLD "%lld"9 #endifTen Const intN =10000+ -; One ll X,m,c,k; A intPos[n]; - /** - * Congruence theorem: the * (a+b) mod n = ((a mod n) + (b mod n)) mod n; - * AB mod n = (a mod n) * (b mod n) mod n; - * - *555 mod n = (550 + 5) mod n = 550 mod n + 5 mod n + * = ((mod n) * (ten mod n) + 5 mod n) mod n; - * g (i) mod n = (g (i-1) * + 5) mod n; + * A * Record the position where the same value appears, find the Loop section, and then approximate I to M, at * TMP continues * + x until m; - * */ - intMain () { - intT,kase =1; -scanf"%d",&T); - while(t--){ inscanf"%lld%lld%lld%lld",&x,&m,&k,&c); -memset (pos,-1,sizeof(POS)); toll tmp =0; +ll d =0, now =0; - for(inti =1; I <= N; i + +){ theTMP = (TMP *Ten+ x)% K;//identical remainder theorem * if(Pos[tmp] = =-1){ $POS[TMP] =i;Panax Notoginseng}Else{ -D = i-pos[tmp]; thenow =i; + Break; A } the } +ll ANSN = Now + (M-now)/d *D; - if(Ansn > m) ansn-=D; $ while(++ANSN <=m) { $TMP = (TMP *Ten+ x)%K; - } - if(TMP = =c) { theprintf"Case #%d:\nyes\n", kase++); -}Else{Wuyiprintf"Case #%d:\nno\n", kase++); the } - } Wu}
Fast power:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 #defineLLD I64d5 ll X,m,c,k;6 ll Quickmod (ll a,ll b,ll MoD) {7LL ret =1;8 while(b) {9 if(B &1) ret = ret*a%MoD;TenA = a*a%MoD; Oneb >>=1; A } - returnret; - } the intMain () { - intT,kase =1; -scanf"%d",&T); - while(t--){ +scanf"%lld%lld%lld%lld",&x,&m,&k,&c); -ll mod =9*K; +LL ret = Quickmod (Ten, m,mod); A if((ret%mod-1%MOD) * (x%mod)%mod = =9*c) { at //if (ret*x%mod-x%mod = = 9*c) { -printf"Case #%d:\nyes\n", kase++); -}Else{ -printf"Case #%d:\nno\n", kase++); - } - } in return 0; - } to /** + * - * (10^m-1)/9*x%k = = C the * (10^m-1) *x/9%k = = C * * * * * due to (10^m-1) *x% 9 = = 0; $ * * * * *----> (10^m-1) *x% (9*k) = = 9*c;Panax Notoginseng * ((10^m% 9*k)-1 9*k) * x% (9*k) mod (9*k) - * */
Hdu 5690 (the same remainder theorem to find the cyclic section/fast power)