Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大侠们, 帮我看看, 不会处理 k == 64, 所以用了高精, 在zoj 过了, 在poj 挂了, %I64d 的也考虑了In Reply To:为什么总是超时,谁有更简单的方法? Posted by:wxc at 2005-08-22 15:26:44 #include<stdio.h> int t,k; long long n; char bit[100]; char N[100]; char Max[100]; char Diff[100]; char Mdiff[100]; bool cmp(char *a, char *b, int k) { if(a[k] == 0 && b[k] == 1) return true; if(a[k] == 1 && b[k] == 0) return false; bool flag = false; for(int i = k-1; i >= 0; i--) { if(a[i] == b[i]) continue; if(a[i] > b[i]) flag = true; else // a[i] < b[i] flag = false; return flag && a[k] == 0 && b[k] == 0 || !flag && a[k] == 1 && b[k] == 1; } return false; } void sub(char *a, char *b, char *c, int k) { int C; if(b[k] == 1) { C = 0; for(int i = 0; i < k; i++) { c[i] = a[i] + b[i] + C; if(c[i] > 1) c[i] -= 2, C = 1; else C = 0; } c[k] = 0; } else // b[k] == 0 { C = 0; for(int i = 0; i < k; i++) { c[i] = 2 + a[i] - b[i] - C; if(c[i] < 2) C = 1; else C = 0, c[i] -= 2; } c[k] = 0; } } int main() { scanf("%d",&t); while(t--) { scanf("%d",&k); scanf("%s",bit); scanf("%I64d",&n); if(n < 0) N[k+1] = 1, n = -n; else N[k+1] = 0; for(int i = 0; i < k; i++) { N[i] = (n&1); n = (n>>1); } Max[k] = Mdiff[k] = Max[k+1] = Mdiff[k+1] = 0; for(int i = 0; i < k; i++) { if(bit[i] == 'p') Max[k-1-i] = 1; else Max[k-1-i] = 0; Mdiff[i] = 1; } sub(Max, N, Diff, k+1); if(cmp(N,Max,k+1)) printf("Impossible\n"); else { if(cmp(Diff,Mdiff,k+1)) { printf("Impossible\n"); } else { for(int i = k-1; i >= 0; i--) { if(Diff[k-1-i] == 1) { if(bit[i] == 'p') bit[i] = '0'; else bit[i] = '1'; } else { if(bit[i] == 'p') bit[i] = '1'; else bit[i] = '0'; } } printf("%s\n",bit); } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator