题目https://nanti.jisuanke.com/t/43375
The famous City Central Zoo houses just two creatures, Zebras and Ocelots. These magical creatures spend their time in a single, tall column, each standing atop the back of the creature below it. Whenever the zoo bell rings, the ocelot lowest in the pile turns into a zebra, and all zebras (if there are any) below that creature simultaneously turn into ocelots. If there is no ocelot in the pile when the bell tolls, then nothing happens. The zookeeper has been watching this interesting process for years. Today, suddenly, he begins to wonder how much longer this can go on. That is, given a pile of zebras and/or ocelots, how many times may the bell toll before there are no more ocelots?
Input consists of a number N in the range 1 to 60, followed by N lines, each of which is a single character, either Z (for zebra) or O (for ocelot). These give the order of the creatures from top (first) to bottom (last).
Output should be a single integer giving the number of times the bell must toll in order for there to be no more ocelots.
规律:用Z表示1,O表示0,这样从上到下组成一个二进制数,用这个数与1<<n -1做差,相差的值的十进制就是答案
但是好像要强制转换longlong在能过,没搞明白为什么。。。
#include <bits/stdc++.h>
using namespace std;
const int maxn=100001;
typedef long long ll;
const double esp=1e-4;
const int mod=1e9+7;
ll ans;int n;
ll sum;ll num;
int main(){
scanf("%d",&n);char ch;
ll add=(ll)1<<(n-1);
//ll add=1<<(n-1); 不强制转换就答案错误
sum=num=0;
for(int i=0;i<n;i++){
getchar();
scanf("%c",&ch);
if(ch=='Z'){
num+=add;
}
sum+=add;
add>>=1;
}
ans=sum-num;
printf("%lld",ans);
return 0;
}