题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5690
题意:m个数字全为x mod k ?= c;其中m <= 1010,0 < c,k <= 10,000;
法1:xxx = (10m-1)/9*x;但是n太大,需要同时mod。去除分母将式子变为:10m*x%(9k) - x%(9k) =? 9c ;其中 10m 快速二次幂即可;
时间复杂度为O(logn)
法2: 由于m个x数的产生对于mod具有可拆分性,所以直接求解周期即可;
#include<bits/stdc++.h> using namespace std; #define rep0(i,l,r) for(int i = (l);i < (r);i++) #define rep1(i,l,r) for(int i = (l);i <= (r);i++) #define rep_0(i,r,l) for(int i = (r);i > (l);i--) #define rep_1(i,r,l) for(int i = (r);i >= (l);i--) #define MS0(a) memset(a,0,sizeof(a)) #define MS1(a) memset(a,-1,sizeof(a)) #define MSi(a) memset(a,0x3f,sizeof(a)) #define inf 0x3f3f3f3f #define lson l, m, rt << 1 #define rson m+1, r, rt << 1|1 typedef pair<int,int> PII; #define A first #define B second #define MK make_pair typedef long long ll; typedef unsigned int uint; template<typename T> void read1(T &m) { T x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} m = x*f; } template<typename T> void read2(T &a,T &b){read1(a);read1(b);} template<typename T> void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);} template<typename T> void out(T a) { if(a>9) out(a/10); putchar(a%10+‘0‘); } const int maxn = 1e4+7; int mp[maxn], rk[maxn]; int main() { //freopen("data.txt","r",stdin); //freopen("out.txt","w",stdout); int T, kase = 1; scanf("%d",&T); while(T--){ MS1(mp); ll x, m, k, c, circle = -1, aux = 0, start; read2(x,m); read2(k,c); rep1(i,1,m){ aux = (aux*10+x)%k; rk[i] = aux; if(mp[rk[i]] != -1){ circle = i - mp[rk[i]]; start = mp[rk[i]]; break; } mp[rk[i]] = i; } //printf("\n%lld %lld\n\n",start,circle); printf("Case #%d:\n",kase++); if(circle == -1) puts(aux == c?"Yes":"No"); else{ int ans = rk[(m - start)%circle + start]; puts(ans == c?"Yes":"No"); } } return 0; }
时间: 2024-10-28 11:02:19