/*Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 325 Accepted Submission(s): 70 Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given two positive integers A and B, output A*B. This problem is even easier than the last one. Alexandra can't wait to give him another task: Given a positive integer A and a string S(S only contains numbers.), find the minimum positive integer B, such that S is a substring of T, where T is the decimal notation of A*B. See the sample for better understanding. Note: S can contain leading zeros, but T can't. Input There are multiple test cases (no more than 500). Each case contains a positive integer A and a string S. A≤10,000,1≤|S|≤8. Output For each case, output the required B. It is guaranteed that such B always exists. To C++ programmers: if you want to output 64-bit integers, please use "%I64d" specifier or cout. Sample Input 6 8 96 19 2 0086 1 1 Sample Output 3 2 5043 1 Source BestCoder Round #19 */ #include<stdio.h> #include<string.h> int main() { long long a; while((scanf("%d",&a))!=EOF) { long long o=0,b=1,n,f,p,t,i,j;char s1[10],s2[100]; //scanf("%d",&a); scanf("%s",s1); n=a; //以下几行是为了确定k的起始位置,为了减少循环。 // printf("(%d)",n); while((n/=10)!=0) o++; f=strlen(s1)-o-1; if(s2[0]==0) f++; // printf("f=%d|",f); while(--f) { if(f==-1) break; b*=10;//printf("*"); } // printf("%d|",b); //printf("%s",s1); for(long long k=b;;k++) { // int flag=0; memset(s2,0,sizeof(s2)); sprintf(s2,"%I64d",k*a); // printf("s2=%s",s2); p=1; //以下几行是用了uvaoj的all in all那题 判断s1是不是s2的子列的代码 t=-2; for(i=0;i<strlen(s1)&&p;i++) { // printf("*"); for(j=0;j<strlen(s2);j++) if(s1[i]==s2[j]&&t<j) { t=j; // printf("(s1[%d]=%c,s2[%d]=%c)",i,s1[i],j,s2[j]); p=0; break; } //printf("%c%c ",s[i],s[j]); p=!p; // memset(s,0,sizeof(s)); // memset(t,0,sizeof(t)); //printf("%c%c",s[1],s[1]); } if(p) {printf("%I64d\n",k);} //printf("s1=%s,s2=%s,k*a=%d,k=%d,a=%d ",s1,s2,k*a,k,a);break;} } } } //最终T了,显然会T,就算没T我测了10000, 74562384.得出的是13388961,显然是错误答案,后来发现是a*k爆了int 。 //我用了longlong型,总算没爆了,但是用了十几秒。 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long inline ten(int n) { long long ans=1; if(n==0) return 1ll*1; for(int i=0;i<n;i++) ans*=10; return ans; } int main() { int a,i; char c[10]; while(~scanf("%d %s",&a,c)) //好方法,按位非,因为EOF的存储方式是 111...所以按位非后就是0,假。 { int now=1,nc=0; for(i=strlen(c)-1;i>=0;i--) { nc+=now*(int)(c[i]-'0'); now*=10; } int lz=0; for(i=0;i<strlen(c);i++) { if(c[i]!='0') break; lz++; } long long ab=-1; long long ans; int len=strlen(c); if(lz) { len++; } int limit=1; for(;;len++) { limit=1; for(int st=0;st<=len-strlen(c)-(lz>0 ? 1:0);st++) { long long base=nc%a; base*=ten(st); base%=a; long long k=ten(strlen(c)+st); long long ed=ten(len-strlen(c)-st); for(i=ed/10;i<ed;i++) { long long need=a-(((i%a)*k)%a+base)%a; if(need==a) need=0; if(need<limit) { long long tmp=i*k+nc*ten(st)+need; ab=ab==-1 ? tmp : min(ab,tmp); } } limit*=10; } if(ab!=-1) break; } ans=ab/a; printf("%I64d\n",ans); } } // 这是比赛的优秀的代码,运行时间150MS. */