嘟嘟嘟html
這道題題面我是看了小半天才懂(太菜了),而後就發現好水啊。ios
只要維護一個棧,存的是t,表明當前的正方形是2t * 2t的,而後從頭開始掃序列,若是遇到2,就把棧頂元素取出來,而後放進去四個t - 1;若是遇到0,就往結果中加入當前棧頂元素t的2t * 2t.git
此題最大範圍是250 * 250,long long也不夠,得開double(long long 和 double雖然都只有64位,但由於儲存凡是不一樣,double範圍卻比long long 大)。ide


1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(' ') 14 #define Mem(a) memset(a, 0, sizeof(a)) 15 typedef long long ll; 16 typedef unsigned long long ull; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const int eps = 1e-8; 20 const int maxn = 205; 21 inline ll read() 22 { 23 ll ans = 0; 24 char ch = getchar(), last = ' '; 25 while(!isdigit(ch)) {last = ch; ch = getchar();} 26 while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();} 27 if(last == '-') ans = -ans; 28 return ans; 29 } 30 inline void write(ll x) 31 { 32 if(x < 0) x = -x, putchar('-'); 33 if(x >= 10) write(x / 10); 34 putchar(x % 10 + '0'); 35 } 36 37 int k; 38 char s[maxn]; 39 db ans = 0; 40 stack<int> st; 41 db quickpow(db a, int b) 42 { 43 db ret = 1; 44 while(b) 45 { 46 if(b & 1) ret *= a; 47 a *= a; b >>= 1; 48 } 49 return ret; 50 } 51 52 int main() 53 { 54 k = read(); 55 scanf("%s", s); 56 int n = strlen(s); 57 st.push(k); 58 for(int i = 0; i < n; ++i) 59 { 60 int t = st.top(); st.pop(); 61 if(s[i] == '2') for(int j = 1; j <= 4; ++j) st.push(t - 1); 62 else if(s[i] == '0') ans += quickpow(2, t) * quickpow(2, t); 63 } 64 printf("%.0lf\n", ans); 65 return 0; 66 }