[AHOI2005]矿藏编码

嘟嘟嘟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 }
View Code