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 |
想了一晚上的题目,觉得挺有意思的,也来贴个ac的代码#include <iostream> #include <fstream> #include <string> using namespace std; #define MAX 1000 #define ONEINT 1000000 struct BigInt { int num[60]; int length; }count[MAX+10]; bool chain[MAX+10]; BigInt add(BigInt a, BigInt b) { int length = a.length; if(b.length > length) length = b.length; int stepin = 0; for(int i = 0; i <= length; ++i) { a.num[i] += stepin+b.num[i]; stepin = a.num[i]/ONEINT; a.num[i] = a.num[i]%ONEINT; } if(a.num[a.length]) ++a.length; return a; } BigInt sub(BigInt a, BigInt b) { for(int i = 0; i < b.length; ++i) { a.num[i] -= b.num[i]; if(a.num[i] < 0) { a.num[i] += ONEINT; a.num[i+1]--; } } while(!a.num[a.length-1] && a.length>1) --a.length; return a; } string Int2String(BigInt a) { int i, j; string out = ""; for(i = 1; i <= a.length; ++i) { string tmp = "000000"; int j = 5; while(a.num[a.length-i]) { tmp[j] += a.num[a.length-i]%10; a.num[a.length-i] /= 10; --j; } out += tmp; } for(i = 0; i < out.length(); ++i) { if(out[i] != '0') break; } return out.substr(i); } BigInt foo(int n) { BigInt current; if(n <= 0) return count[0]; current = count[n]; //cout << Int2String(current) << endl; --n; while(n>=0 && !chain[n]) --n; if(n >= 0) current = sub(current, foo(n)); return current; } int main() { BigInt one; one.length = 1; memset(one.num, 0, sizeof(one.num)); one.num[0] = 1; int N; cin >> N; memset(count, 0, sizeof(count)); count[0].length = 1; count[0].num[0] = 1; for(int n = 1; n <= N; ++n) { count[n] = add(count[n-1], count[n-1]); count[n] = add(count[n], one); //cout << Int2String(count[n]) << endl; cin >> chain[n-1]; } --N; while(N>=0 && !chain[N]) --N; if(N >= 0) cout << Int2String(foo(N)) << endl; else cout << 0 << endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator