int main() { Bigint arr[5],tmp[5]; int T; scanf("%d",&T); while(T--) { arr[1] = "1"; arr[2] = "1"; arr[3] = "1"; arr[4] = "1"; int N; scanf("%d",&N); if(N == 0) { printf("0\n"); continue; } for(int i = 2 ; i <= N ; i++) { tmp[1] = arr[1]; tmp[2] = arr[2]; tmp[3] = arr[3]; tmp[4] = arr[4]; arr[1] = "0"; arr[2] = "0"; arr[3] = "0"; arr[4] = "0"; arr[2] = arr[2] + tmp[1]; arr[1] = arr[1] + tmp[2]; arr[3] = arr[3] + tmp[2]; arr[4] = arr[4] + tmp[3]; arr[3] = arr[3] + tmp[4]; arr[2] = arr[2] + tmp[4]; } //printf("%d\t%d\t%d\t%d\t\n",arr[1],arr[2],arr[3],arr[4]); Bigint sum = arr[1]+arr[2]+arr[3]+arr[4]; sum.print(); printf("\n"); } return 0; }
Bigint::Bigint(const Bigint &big) { int nlen = strlen(big.getptr()); m_pnum = new char[nlen +1]; memset(m_pnum, 0, nlen + 1); strcpy_s(m_pnum, nlen +1, big.getptr()); }
int cp3(const Bigint &b)const { if (s != b.s) return s > b.s; if (s == -1) return -(-*this).cp3(-b); if (len() != b.len()) return len()>b.len()?1:-1; for (int i=len()-1; i>=0; i--) if (v[i]!=b.v[i]) return v[i]>b.v[i]?1:-1; return 0; }
int cp3(const Bigint &b)const { if (s != b.s) return s - b.s; if (s == -1) return -(-*this).cp3(-b); if (len() != b.len()) return len()-b.len();//int for (int i=len()-1; i>=0; i--) if (v[i]!=b.v[i]) return v[i]-b.v[i]; return 0; }
pair<Bigint, int> divmod(Bigint a, int m) { long long r = 0; for (int i = a.len() - 1; i >= 0; i--) { r = r * BASE + a[i]; a[i] = r / m; r %= m; } a.trim(); return make_pair(a, (int) r); }
Bigint operator + ( Bigint b ) { if( sign != b.sign ) return (*this) - b.inverseSign(); Bigint c; for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) { carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0); c.a += (carry % 10 + 48); carry /= 10; } return c.normalize(sign); }
int bigcmp(const Bigint& num1, const Bigint& num2) { int nlen1 = num1.getlength(), nlen2 = num2.getlength(); if(nlen1 > nlen2) return 1; if(nlen1 < nlen2) return -1; return strcmp(num1.getptr(), num2.getptr()); }
Bigint operator - ( Bigint b ) { if( sign != b.sign ) return (*this) + b.inverseSign(); if( (*this) < b ) return (b - (*this)).inverseSign(); Bigint c; for( int i = 0, borrow = 0; i < (int)a.size(); i++ ) { borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48); c.a += borrow >= 0 ? borrow + 48 : borrow + 58; borrow = borrow >= 0 ? 0 : 1; } return c.normalize(sign); }
Bigint operator * (Bigint a, int m) { long long r = 0; for (int i = 0; i < a.len(); i++) { r += (long long) a[i] * m; a[i] = r % BASE; r /= BASE; } while (r) a.data.push_back(r % BASE), r /= BASE; a.trim(); return a; }
Bigint operator - ( Bigint b ) { // subtraction operator overloading if( sign != b.sign ) return (*this) + b.inverseSign(); int s = sign; sign = b.sign = 1; if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s); Bigint c; for( int i = 0, borrow = 0; i < a.size(); i++ ) { borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48); c.a += borrow >= 0 ? borrow + 48 : borrow + 58; borrow = borrow >= 0 ? 0 : 1; } return c.normalize(s); }
int main() { Bigint n, m; string a, b; while (cin >> a >> b) { n = a; m = b; Bigint sum; sum = fact(n) + fact(m); sum.print(); cout << endl; } return 0; }
Bigint operator * (const Bigint& a, const Bigint& b) { Bigint res; res.data.assign(a.data.size() + b.data.size(), 0); for (int i = 0; i < a.data.size(); i++) { long long carry = 0; for (int j = 0; j < b.data.size() || carry > 0; j++) { long long s = res[i + j] + carry + (long long) a.data[i] * (j < b.data.size() ? (long long) b.data[j] : 0); res[i + j] = s % BASE; carry = s / BASE; } } res.trim(); return res; }
void Bigint::internalDivide(const Bigint& number, const bool& isDecimal, const int& precision) { std::string mstring; std::vector<int> mparts; std::string fnvalue = toString(); std::string snvalue = number.toString(); if(fnvalue.length()>snvalue.length()) { if(!number.isPositive) mstring = snvalue.substr(1); else mstring = snvalue; mparts = parts; } else { if(!number.isPositive) mstring = fnvalue.substr(1); else mstring = fnvalue; mparts = number.parts; } if(mstring!="" && mparts.size()>0) { int recurse = 0; std::string build; decompose(fnvalue, snvalue, number, recurse, build, isDecimal, precision); try { create(build); } catch (...) { } } }
Bigint operator * (const Bigint &b) { Bigint r; r.resize(len() + b.len() + 1); r.s = s * b.s; for (int i=0; i<len(); i++) { for (int j=0; j<b.len(); j++) { r.v[i+j] += v[i] * b.v[j]; if(r.v[i+j] >= BIGMOD) { r.v[i+j+1] += r.v[i+j] / BIGMOD; r.v[i+j] %= BIGMOD; } } } r.n(); return r; }
Bigint operator - (const Bigint &b) const { if(s == -1) return -(-(*this)-(-b)); if(b.s == -1) return (*this)+(-b); if((*this)<b) return -(b-(*this)); Bigint r; r.l=l; for(int i=0;i<l;i++) { r.v[i]+=v[i]; if(i<b.l) r.v[i]-=b.v[i]; if(r.v[i]<0) { r.v[i]+=bM; r.v[i+1]--; } } r.n(); return r; }
Bigint operator + (const Bigint &b) const { if (s == -1) return -(-(*this)+(-b)); if (b.s == -1) return (*this)-(-b); Bigint r; int nl = max(len(), b.len()); r.resize(nl + 1); for (int i=0; i<nl; i++) { if (i < len()) r.v[i] += v[i]; if (i < b.len()) r.v[i] += b.v[i]; if(r.v[i] >= BIGMOD) { r.v[i+1] += r.v[i] / BIGMOD; r.v[i] %= BIGMOD; } } r.n(); return r; }
Bigint operator * ( Bigint b ) { int MAXN=a.size()+b.size()+5; int tmp[MAXN]; memset(tmp,0,sizeof(tmp)); for(int i=0; i<a.size(); i++) for(int j=0, p=i; j<b.size(); j++){ tmp[p++] += (a[i]-'0')*(b.a[j]-'0'); } Bigint c; for(int i=0; i<MAXN-1; i++){ tmp[i+1] += tmp[i]/10; tmp[i] %= 10; c.a.push_back(tmp[i]+'0'); } return c.normalize(sign*b.sign); }
int main() { //READ("in.txt"); //WRITE("out.txt"); Bigint one; one.a="1"; one.print(); pre[0]=one; Bigint j=one; for(int i=1;i<=100;i++,j=j+one) { debug(i) pre[i]=pre[i-1]*j; debug(i) pre[i].print(); } return 0; }
Bigint operator % ( Bigint b ) { if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ); Bigint c("0"); b.sign = 1; for( int i = a.size() - 1; i >= 0; i-- ) { c.a.insert( c.a.begin(), '0'); c = c + a.substr( i, 1 ); while( !( c < b ) ) c = c - b; } return c.normalize(sign); }
Bigint operator / ( Bigint b ) { if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ); Bigint c("0"), d; for( int j = 0; j < a.size(); j++ ) d.a += "0"; int dSign = sign * b.sign; b.sign = 1; for( int i = a.size() - 1; i >= 0; i-- ) { c.a.insert( c.a.begin(), '0'); c = c + a.substr( i, 1 ); while( !( c < b ) ) c = c - b, d.a[i]++; } return d.normalize(dSign); }
Bigint operator / (const Bigint &b) { Bigint r; r.resize(max(1, len()-b.len()+1)); int oriS = s; Bigint b2 = b; // b2 = abs(b) s = b2.s = r.s = 1; for (int i=r.len()-1; i>=0; i--) { int d=0, u=BIGMOD-1; while(d<u) { int m = (d+u+1)>>1; r.v[i] = m; if((r*b2) > (*this)) u = m-1; else d = m; } r.v[i] = d; } s = oriS; r.s = s * b.s; r.n(); return r; }
Bigint operator + (Bigint a, Bigint b) { if (a.len() < b.len()) swap(a, b); Bigint c; c.data = a.data, c.data.push_back(0); int r = 0; for (int i = 0; i < c.len(); i++) { c[i] += ((i < b.len()) ? b[i] : 0) + r; if ((r = (c[i] >= BASE))) c[i] -= BASE; } c.trim(); return c; }
int main() { FILEIO("journey"); cin >> N; for (int i=1, M; i<=N; i++) { cin >> M; for (int j=0; j<M; j++) { string str; cin >> str; if (str[0] == 'F') { str = str.substr(1, string::npos); } f[i].ins.PB(str); } } FZ(done); FZ(onstack); go(1, 0, 0, 0); ans.print(); puts(""); return 0; }
bool operator<(const Bigint &lhs, const Bigint &rhs) { if(lhs.compare(rhs)==-1) return true; return false; }
bool operator>=(const Bigint &lhs, const Bigint &rhs) { if(lhs.compare(rhs)>=0) return true; return false; }
//right param is litter one Bigint operator *(Bigint num1,Bigint num2) { #if 1 static bool s_first = 1; if(num2.getlength() == 0|| num1.getlength() ==0|| strcmp(num2.getptr(), "0") == 0 || strcmp(num1.getptr(), "0") == 0) return "0"; if (s_first && num1<num2){num1.swap(num2);} s_first = 0; #endif int nlen1 = num1.getlength(); int nlen2 = num2.getlength(); //memory leak ptr_array<char> sum = new char[nlen1+1+1]; memset(sum.getptr() , 0, nlen1+2); int flag = 0; int nindex = 0; for(; nindex < nlen1; nindex++) { int num = (num2.getindex(nlen2-1)-'0')*(num1.getindex(nlen1 - nindex - 1)-'0') +flag; sum[nindex] = num%10 +'0'; flag = num/10; } if(flag) sum[nindex] = flag +'0'; makestr(sum.getptr()); //memory leak ptr_array<char> pleft =new char[nlen2+1]; strcpy(pleft.getptr(), num2.getptr()); pleft[num2.getlength() - 1] = 0; // Bigint temp =ten(num1*pleft); return (Bigint(sum.getptr()) + ten(num1*pleft.getptr())); }
Bigint operator+(const Bigint& num1, const Bigint& num2) { int nlen1 = num1.getlength(); int nlen2 = num2.getlength(); const Bigint &maxint = nlen1>=nlen2?num1:num2; const Bigint &minint = nlen1>=nlen2?num2:num1; //申请的字节数永远比最长的数多1.但计算的时候按不进位计算 //memory leak ptr_array<char> ptr(new char[maxint.getlength()+1+1]); Bigint temp = ptr.getptr(); memset(temp.getptr(), 0, maxint.getlength()+1+1); int maxindex = maxint.getlength() -1; int minindex = minint.getlength() -1; int nindex = strlen(maxint.getptr()) -1; int nmax = maxint.getlength(); int nmin = minint.getlength(); bool flag = false; for(int max = maxint.getlength(); max>0; max--) { temp.getindex(nindex) = (maxint.getindex(maxindex--) + (minindex>=0?minint.getindex(minindex--):'0') +flag -'0'); flag = false; if( temp.getindex(nindex--)> '9') { flag = true; temp.getindex(nindex+1) = (temp.getindex(nindex+1) % ('9'+1)) +'0'; } } if(flag) { for (int i=temp.getlength(); i>0; i--) { temp.getindex(i) = temp.getindex(i-1); } temp.getindex(0) = '1'; } return temp; }
int Bigint::compare(const Bigint& number1, const Bigint& number2) { return number1.compare(number2); }
void go(int id, Bigint x, Bigint y, int dir) { if (onstack[id][dir]) { if (x != location[id][dir].F || y != location[id][dir].S) { puts("Infinity"); } else { for (int i=1; i<=N; i++) { for (int j=0; j<4; j++) { ans = max(ans, dp[i].v[j]); } } ans.print(); puts(""); } exit(0); } if (done[id][dir]) return; onstack[id][dir] = 1; Bigint oriX = x; Bigint oriY = y; location[id][dir] = {x, y}; dp_t best; int oriD = dir; for (auto it : f[id].ins) { if ('0' <= it[0] && it[0] <= '9') { int j = atoi(it.c_str()); go(j, x, y, dir); dp_t tmp = dp[j]; for (int i=0; i<4-dir; i++) { rotateR(tmp); } Bigint tx = x - oriX; Bigint ty = y - oriY; for (int i=0; i<4; i++) { ans = max(ans, tmp.v[i] + x.sign(sign[i][0]) + y.sign(sign[i][1])); best.v[i] = max(best.v[i], tmp.v[i] + tx.sign(sign[i][0]) + ty.sign(sign[i][1])); } x = x + mov[j][dir].F; y = y + mov[j][dir].S; dir = (dir + movD[j]) % 4; } else { if (it == "LEFT") { dir++; } else if (it == "RIGHT") { dir--; } else { x = x + dirMove[dir][0]; y = y + dirMove[dir][1]; } Bigint tx = x - oriX; Bigint ty = y - oriY; for (int i=0; i<4; i++) { best.v[i] = max(best.v[i], tx.sign(sign[i][0]) + ty.sign(sign[i][1])); } dir = (dir + 4) % 4; } ans = max(ans, absBigint(x) + absBigint(y)); } mov[id][oriD] = {x - oriX, y - oriY}; movD[id] = (4 + dir - oriD) % 4; back0(best, oriD); dp[id] = best; done[id][oriD] = 1; onstack[id][oriD] = 0; }
void Bigint::multiply(const Bigint& number) { Bigint mulResult; std::string mstring; std::vector<int> mparts; std::string fnvalue = toString(); std::string snvalue = number.toString(); if(fnvalue.length()>snvalue.length()) { if(!number.isPositive) mstring = snvalue.substr(1); else mstring = snvalue; mparts = parts; } else { if(!number.isPositive) mstring = fnvalue.substr(1); else mstring = fnvalue; mparts = number.parts; } if(mstring!="" && mparts.size()>0) { int position = 0; for (int i = mstring.length(); i > 0 ; i--, position++) { std::string numstr = BLANK; int mult = mstring.at(i-1) - '0'; int carryOver = 0; for (int j=0;j<(int)mparts.size();j++) { std::string res = CastUtil::lexical_cast<std::string>(mparts.at(j)*mult); if(j!=(int)mparts.size()-1) { std::string mrtn = CastUtil::lexical_cast<std::string>(mparts.at(j)); if(res.length()>mrtn.length()) { int numm = CastUtil::lexical_cast<int>(res.substr(1)) + carryOver; numstr = CastUtil::lexical_cast<std::string>(numm) + numstr; carryOver = res.at(0) - '0'; } else { int numm = CastUtil::lexical_cast<int>(res) + carryOver; numstr = CastUtil::lexical_cast<std::string>(numm) + numstr; carryOver = 0; } if(j==0) { int nl = numstr.length(); for (int jj = 0; jj < NUM_LENGTH-nl; jj++) { numstr = ZERO + numstr; } } } else { int numm = CastUtil::lexical_cast<int>(res) + carryOver; numstr = CastUtil::lexical_cast<std::string>(numm) + numstr; carryOver = 0; } } for (int j = 0; j < position; j++) { numstr += ZERO; } try { Bigint num(numstr); mulResult.internalAdd(num); } catch (...) { } } } this->parts = mulResult.parts; if(isPositive!=number.isPositive) { isPositive = false; } else if(isPositive==number.isPositive) { isPositive = true; } }