#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include//assert(); #include ///////// #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i,n) REP(i,0,n) #define P(p) cout<<(p)< ///////// #ifdef getchar_unlocked #define mygc(c) (c)=getchar_unlocked() #else #define mygc(c) (c)=getchar() #endif #ifdef putchar_unlocked #define mypc(c) putchar_unlocked(c) #else #define mypc(c) putchar(c) #endif ///////// typedef long long LL; typedef long double LD; typedef unsigned long long ULL; ///////// using namespace::std; ///////// /////数値読み込み #define ENABLE_READER_ON(T) \ inline void reader(T &x){\ int k;x = 0;bool flag = true;\ while(true){\ mygc(k);\ if( k == '-'){\ flag = false;break;\ }\ if('0' <= k && k <= '9'){\ x = k - '0';break;\ }\ }\ if( flag ){\ while(true){\ mygc(k);\ if( k<'0' || '9'> 1) & 0x5555555555555555ULL; N = ((N >> 2) & 0x3333333333333333ULL) + (N & 0x3333333333333333ULL); N = ((N >> 4) + N) & 0x0F0F0F0F0F0F0F0FULL; return (N * 0x0101010101010101ULL) >> 56; } /* CPythonのソース参照 longobject.c 以下は */ const int PyLong_SHIFT (30); //#define PyLong_SHIFT (15) const unsigned long long PyLong_BASE ((ULL)1< A*base^1 +b*base^0 unsigned long long convmax = base; int i= 1; log_base_BASE[base] =(log((double)base)/ log((double)PyLong_BASE)); for(;;){ unsigned long long next = convmax * base; //PyLong_BASE進数 if( next > PyLong_BASE ){ break; } convmax = next; ++i; } convmultmax_base[base] = convmax; assert(i>0); convwidth_base[base] = i;//桁数保存 } /* log_base_BASE[10] = 0.110731 convmultmax_base[10] = 10^9 convwidth_base[10] = 9 */ //文字列の長さは取得できる。 digits = strlen(str); //必要な容量の計算 size_z = (unsigned long long)(digits * log_base_BASE[base]) + 1; vector z((unsigned int)size_z,0);//上限unsigned int convwidth = convwidth_base[base]; convmultmax = convmultmax_base[base]; /* Work (^o^)*///かわいい顔文字をここへ unsigned long long calNum; const int len = strlen(str); int pos,posMax;// posMax = 0;//値を格納してるMaxIndex...名前 int strPos = 0;//strの位置 while(strPos 1;--pos){ convmult *= base; } } //z[]に値を入れていく。0初期化 /* 元ソースのコメントにある (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 括弧の一番奥から計算していく、最後は調整のため掛けるBも合わせてる。 */ for(pos = 0; pos < posMax+1;++pos){ calNum += z[pos] * convmult; z[pos] = calNum & PyLong_MASK; calNum >>= PyLong_SHIFT; } //繰り上がりの処理、1loop1回は来る。 if(calNum){//頻繁に来る assert(calNum < PyLong_BASE); //次の桁に入れる。 if( (unsigned)pos >= z.size() ){//容量不足 z.push_back( calNum ); }else{ z[pos] = calNum;//容量の不安はある } posMax = max(posMax,pos); } } unsigned long long ret = 0; int i=0; const ULL sizeMax = z.size(); for(i=0; i