题意
每秒钟2后面加一个1,1后面加1个0,然后删除第一个字符,问需要多少秒才能删完、
题解
可以发现:0会使答案加1,1会使答案T*2+2,2会使答案T变成(2^(T+1)-1)*3,所以一边计算即可,但是由于要取模,所以需要扩展欧拉定理,证明转载自http://blog.csdn.net/synapse7/article/details/19610361,我也就不在这说了,自己也不太懂;考虑到可能会TLE,所以需要提前保存需要用到的欧拉值,然后逆着做一遍,参考着做题。
扩展欧拉定理
转载自https://blog.csdn.net/abutoto/article/details/81264109
AC代码
#include<bits/stdc++.h>
#define N 2000005
#define P pair<int,int>
using namespace std;
typedef long long ll;
const int M=1e9+7;
const int MM=1e9+6;
const int inf=1e9+7;
ll Mod(ll x,ll y){
return x < y ? x : x % y + y;
}
long long euler(long long n)
{
ll m = sqrt(n+0.5);
ll ans = n;
for(int i=2; i<=m; ++i) if(n%i == 0)
{
ans = ans/i * (i-1);
while(n%i == 0) n /= i;
}
if(n > 1) ans = ans/n * (n-1);
return ans;
}
ll quick(ll a,ll b,int mod)
{
ll c=1;
while(b){
if(b&1)c=Mod(c*a,mod);
a=Mod(a*a,mod);
b>>=1;
}
return c;
}
char s[N];
vector<int>phi;
ll solve(int n,int m,int d)
{
if(n<=0)return 0;
if(s[n]=='2'){
if(d>=phi.size())d=phi.size()-1;
ll t=solve(n-1,phi[d],d+1);
t=quick(2,t+1,m);
return Mod(3*(t-1),m);
}
if(s[n]=='0')return Mod(solve(n-1,m,d)+1,m);
return Mod(solve(n-1,m,d)*2+2,m);
}
int main()
{
int t;
int k=M;
while(k!=1){
k=euler(k);
phi.push_back(k);
}
for(scanf("%d",&t);t;t--)
{
scanf("%s",s+1);
int n=strlen(s+1);
printf("%lld\n",solve(n,M,0)%M);
}
return 0;
}
/*
3
000
012
22
*/