题:https://www.nowcoder.com/acm/contest/142/A
思路:对于每一个字符我们只需要考虑需要多久时间才能处理到他以及处理到他的时候他对ans的贡献是多少,计算可以得出当前是0的时候 ,ans++,是1的时候ans=2(ans+1) ,是2的时候 ans=2^ans-3,处理到这里只剩下最后一个问题 求a^(b^(c^d……)) 不能直接对指数取模1e9+7,这时每当处理到一个2时就需要求欧拉(mod),这样倒着算一遍就能得出ans
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
char a[100007];
ll c[50];
string b;
map<ll,ll>mp;
ll qmod(ll a, ll b,ll p) {
ll s = 1;
while (b > 0) {
if (b % 2 == 1) {
s = s * a%p;
}
a = a * a%p;
b = b >> 1;
}
return s%p;
}
ll eular(ll n)
{
ll ret=1;
for(int i=2;i*i<=n;i++){
if(n%i==0){
n/=i;
ret*=i-1;
while(n%i==0){
ret*=i;
n/=i;
}
}
}
if(n>1) ret*=n-1;
return ret;
}
int cnt=0;
void init() {
ll temp=1e9+7;
while(temp!=1) {
mp[temp]=eular(temp);
temp=mp[temp];
}
mp[temp]=1;
}
ll solve(int p,ll M) {
if(p==-1) return 0;
if(M==1) return 0;
if(a[p]=='2') {
ll temp=solve(--p,mp[M]);
return (6ll*qmod(2,temp,M)-3+M)%M;
}
else if(a[p]=='1') {
return 2ll*(solve(--p,M)+1)%M;
}
else{
return (1+solve(--p,M))%M;
}
}
int main()
{
int n;
scanf("%d",&n);
init();
/*for(int i=0;i<cnt;i++) {
cout<<c[i]<<endl;
}*/
while(n--){
scanf("%s",a);
long long ans=0;
int len=strlen(a);
int pos=-1;
ans=solve(len-1,MOD);
printf("%lld\n",ans%MOD);
}
return 0;
}
/*
3
000
012
22
*/