比赛时候脑子堵得慌,注意进位,这两场比赛都发挥的很不好
#include <iostream> #include <cstdio> #include <cstring> using namespace std; ////////////////////////////////////// #define MAX 10000+123 #define mod 10 #define baselen 1 #define in(a) scanf("%d",&a) #define out1(a) printf("%d",a) typedef int type; ///////////////////////////////////// char str1[MAX], str2[MAX]; int t = 0; struct bint { type dig[MAX], len; bint() { len = 0, dig[0] = 0; } }; //////////////////////////////////////////// bool input(bint& a) { type i, j, w, k, p; char data[MAX*baselen+1]; if (scanf("%s",data)==EOF)return false; if(t == 0) strcpy(str1, data); else strcpy(str2, data); w = strlen(data) - 1, a.len = 0; for (p=0;p<=w&&data[p]=='0';p++); while (1) { i = j = 0, k = 1; while (i<baselen&&w>=p) { j = j+ (data[w--] - '0')*k; k *= 10, i++; } a.dig[a.len++] = j; if (w<p)break; } a.len--; return true; } void output(bint& a) { type i; i = a.len - 1; out1(a.dig[a.len]); while(i>=0)out1(a.dig[i--]); } void sub(bint a, bint b, bint& c) { type i, carry; for ( i=carry=0; i<=a.len; i++) { c.dig[i] = a.dig[i]-carry; if (i<=b.len)c.dig[i] -= b.dig[i]; if (c.dig[i]<0)carry = 1, c.dig[i] += mod; else carry = 0; } i--; while (i&&c.dig[i]==0)i--; c.len = i; } int cmp(bint a, bint b) { if (a.len<b.len)return -1; if (a.len>b.len)return 1; int i = a.len; while (i&&a.dig[i]==b.dig[i])i--; return a.dig[i] - b.dig[i]; } void give(bint a, bint& b) { int i = 0; while (i<=a.len) { b.dig[i] = a.dig[i]; i++; } b.len = a.len; } void add(bint a, bint b, bint& c) { type i, carry ; for ( i = carry = 0; i <= a.len || i <= b.len || carry; i++) { if (i<=a.len)carry += a.dig[i]; if (i<=b.len)carry += b.dig[i]; c.dig[i] = carry%mod; carry /= mod; } c.len = i - 1; } int i,j; int cases,nc; int main() { scanf("%d",&cases); for (nc=1;nc<=cases;++nc) { bint ans; bint a,b,c,d; ans.len=10000; ans.dig[10000]=1; t = 0; input(a); t = 1; input(b); /*bool ff; bool flag= false; //printf("a.len =%d b.len = %d\n", a.len, b.len); for(int i = 0; i <= a.len - b.len; i++) { ff = false; for(int j = 0; j <= b.len; j++) { if(a.dig[i + j] != b.dig[j]) { ff = true; break; } } if(!ff) { flag = true; break; } } if(flag) { printf("Case #%d: ", nc); printf("0\n"); continue; }*/ //int ptr = strstr(a.dig, b.dig); char *ptr = strstr(str1, str2); if(ptr != NULL) { printf("Case #%d: ", nc); printf("0\n"); continue; } for (i=b.len; i <= b.len + a.len + 1;++i) { give(a,c); if (i>=a.len) c.len=i; int tl=b.len; // if(i == b.len ) // { // output(c); // puts(""); // } for (j=i;j>=0;--j,--tl) { if (tl>=0) c.dig[j]=b.dig[tl]; else c.dig[j]=0; } //if(i == b.len ) //{ // output(c); // puts(""); //} if (cmp(c,a)>=0) { sub(c,a,d); if (cmp(d,ans)<0) give(d,ans); //break; } else { if(c.len < i + 1) { c.dig[i + 1] = 1; c.len++; } else { c.dig[i + 1] += 1; int tem = i + 1; while(c.dig[tem] == 10) { if(c.len < tem + 1) { c.dig[tem + 1] = 1; c.dig[tem] = 0; c.len ++; } else { c.dig[tem + 1] += 1; c.dig[tem] = 0; } tem++; } } //if(i == b.len) //output(c); //puts(""); sub(c,a,d); if (cmp(d,ans)<0) give(d,ans); } } printf("Case #%d: ", nc); output(ans); puts(""); } return 0; }