牛客多校第四场 A Ternary String(dfs + 递推 + 欧拉降幂)

链接:https://www.nowcoder.com/acm/contest/142/A
来源:牛客网
 

题目描述

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 become ``11021'' 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).

输入描述:

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).
It is guaranteed that the sum of all |s| does not exceed 2 x 106.

输出描述:

For each test case, output an integer denoting the answer. If the string never becomes empty, output -1 instead.

题意:给个长度为n的三进制串,有这样一个操作:在每个2后面插入一个1,每个1后面插入一个0,然后删掉第一个字符。问多少次操作后,变成空串。

思路:对于'0','1','2',分别讨论删除情况,假定当前操作次数为x,显然到'0'时,x=x+1,到'1'时,x=2*x+2,但是到了'2'就需要推了,生成的'1'为对比数列的和,'0'可以将'1'带进去。求得的值为3*2^x mod 1e9+7。剩下的东西用欧拉降幂可以轻易搞定。

#include 
using namespace std;
const long long mod = (long long)1e9 + 7;
const int N = 1e5 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
typedef unsigned long long ull;
typedef long long ll;
typedef double dl;
#define mem(a) memset(a,0,sizeof a)
inline ll gcd(ll a,ll b) {return b ? gcd(b,a % b) : a;}
inline ll lcm(ll a,ll b) {return a / gcd(a,b) * b;}
inline double Euler(int n) {return log(n) + 1.0 / (2 * n) + 0.57721566490153286060651209;}
//inline ll inverse_feima(ll n) {return fpow(n,mod - 2);}
ll n,m,k,t,ans = 0;
char s[N];
bool flag,tag;
map map_phi;
ll get_phi(ll n)
{
    ll res = n,a = n;
    for(ll i = 2;i * i <= n;++i){
        if(a % i == 0){
            res = res / i * (i - 1);
            while(a % i == 0)    a /= i;
        }
    }
    if(a > 1) res = res / a * (a - 1);
    return res;
}
ll fpow_mod(ll a,ll b,ll mod)
{
    ll res = 1;
    while(b){
        if(b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}
void init(ll mod)
{
    while(mod != 1){
        map_phi[mod] = get_phi(mod);
        mod = map_phi[mod];
    }
    map_phi[1] = 1;
}
ll dfs(ll pos,ll mod)
{
    if(pos == 0) return 0;
    else if(s[pos] == '0') return (dfs(pos - 1,mod) + 1) % mod;
    else if(s[pos] == '1') return (2 * dfs(pos - 1,mod) + 2) % mod;
    else return (3 * fpow_mod(2,dfs(pos - 1,map_phi[mod]) + 1,mod) + mod - 3) % mod;
}
int main()
{
    //ios::sync_with_stdio(0);
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    scanf("%lld",&t);
    init(mod);
    while(t--){
        scanf("%s",s + 1);
        ll len = strlen(s + 1);
        ll ans = dfs(len,mod);
        printf("%lld\n",ans);
    }
    return 0;
}

 

你可能感兴趣的:(dfs,数论)