牛客网暑期ACM多校训练营(第四场)A:Ternary String(扩展欧拉定理降幂)

链接:https://www.nowcoder.com/acm/contest/142/A

时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
A ternary string is a sequence of digits, where each digit is either 0, 1, or 2.
Chiaki has a ternary string s which can self-reproduce. Every second, a digit 0 is inserted after every 1 in the string, and then a digit 1 is inserted after every 2 in the string, and finally the first character will disappear.
For example, 212'' will become11021” after one second, and become “01002110” after another second.
Chiaki would like to know the number of seconds needed until the string become an empty string. As the answer could be very large, she only needs the answer modulo ( 109+7 10 9 + 7 ).
输入描述:
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains a ternary string s(1|s|105). s ( 1 ≤ | s | ≤ 10 5 ) .
It is guaranteed that the sum of all |s| | s | does not exceed 2 2 x 106 10 6 .
输出描述:
For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.
示例1
输入
3
000
012
22
输出
3
93
45
思路:记录时间 t t [1,i] [ 1 , i ] 之间的字符完全消失的时间,那么在某个时间 t t 时,碰到的字符为 2 ′ 2 ′ ,那么这个字符消失的时间为 t=32t+13 t = 3 ∗ 2 t + 1 − 3 ;若碰到的字符为 0 ′ 0 ′ t=t+1 t = t + 1 ;若碰到的字符为 1 ′ 1 ′ t=2t+2 t = 2 ∗ t + 2
那么接下来就是按照顺序一个一个来更新答案就行了。
但是中间的取模过程就有问题了。
比如碰到字符 2 ′ 2 ′ 的时候, t=32t+13 t = 3 ∗ 2 t + 1 − 3 。则之前的 t t 不能取模。但是 t t 又可能会非常的大,所以这时候就要用到扩展欧拉定理来降幂了。
axmod a x m o d P=axmodϕ(p)+(x>ϕ(p)?ϕ(p):0)modP P = a x m o d ϕ ( p ) + ( x > ϕ ( p ) ? ϕ ( p ) : 0 ) m o d P

#include
using namespace std;
const int MAX=1e5+10;
const int MOD=1e9+7;
typedef long long ll;
mapp;
ll POW(ll x,ll n,ll mod)
{
    ll res=1;
    while(n)
    {
        if(n&1)res=res*x%mod;
        x=x*x%mod;
        n/=2;
    }
    return res;
}
ll f(int x)        //求x的欧拉函数值
{
    ll ans=x;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans/=i;
            ans*=i-1;
            while(x%i==0)x/=i;
        }
    }
    if(x>1)
    {
        ans/=x;
        ans*=x-1;
    }
    return ans;
}
int check(ll x)
{
    ll a=1;
    for(int i=1;i<=x;i++)
    {
        a*=2;
        if(3*a-3>=1e9+7)return 1;
    }
    return 0;
}
ll tag;
char s[MAX];
ll dfs(int x,ll mod)
{
    if(x==0)return 0;
    ll ans=0;
    if(s[x]=='0'||s[x]=='1')ans=dfs(x-1,mod);
    if(s[x]=='2')ans=dfs(x-1,p[mod]);
    if(tag)
    {
        if(s[x]=='0')(ans+=1)%=mod;
        if(s[x]=='1')ans=2*(ans+1)%mod;
        if(s[x]=='2')ans=3*POW(2,(ans+1)%p[mod]+p[mod],mod)%mod-3;
        ans%=mod;
        (ans+=mod)%=mod;
    }
    else
    {
        if(s[x]=='0')ans++;
        if(s[x]=='1')ans=2*(ans+1);
        if(s[x]=='2')
        {
            ans++;
            int flag=0;
            if(ans>=1e9+7)tag=1;
            if(check(ans))flag=1;
            ans=3*POW(2,ans%p[mod]+tag*p[mod],mod)%mod-3;
            tag|=flag;
            ans%=mod;
            (ans+=mod)%=mod;
        }
        if(ans>=1e9+7)
        {
            tag=1;
            ans%=mod;
        }
    }
    return ans;
}
int main()
{
    p[1]=1;
    ll mod=1e9+7;
    while(mod!=1){p[mod]=f(mod),mod=p[mod];}
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%s",s+1);
        int n=strlen(s+1);
        tag=0;
        printf("%lld\n",dfs(n,MOD));
    }
    return 0;
}

你可能感兴趣的:(数学)