1. 程式人生 > >牛客暑假多校——A-Ternary String(找規律+尤拉降冪)(模板)

牛客暑假多校——A-Ternary String(找規律+尤拉降冪)(模板)

題目描述

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.

示例1

輸入

複製

3
000
012
22

輸出

複製

3
93
45

思路:

-1的狀況顯然是不存在的。

前提:之前已已經操作了 t 秒,當前位置是***

①:當前位置是1:過了t秒,還需要刪掉     2*t+2    次才能刪完

②:當前位置是0:那麼還要操作      t+1        次才能刪完

三:當前位置是2,那麼還要操作      3*2^{t+1}-3   次才能刪完

打表:

1
2
01
4
001
6
0001
8
00001
10
000001
12
0000001
14

2
3
02
9
002
21
0002
45
00002
93
000002
189

之所以想到這樣打表,是因為0的話一秒刪一個。這樣子就可以輕易得出過了t秒後,刪掉當前1或者2需要用多少秒。

然後就是計算了。利用dfs 先把之前的用了多少秒算出來,然後可以發現求冪的時候再取模這個數是非常大的,不精準。這樣就想到了尤拉降冪。我也是看了題解才知道的。

下面是大佬的解釋:

不過由於計算量太大,需要用到取模運算,對於0和1公式都只存在加和乘運算,對結果產生不了影響,可是對於2的時候存在求2^{t+1}的運算,然後以該結果作為後面運算的t來運算,模運算的結果發生了變化,若是不會發生變化,都知道是用快速冪來計算,可是為了消除冪運算後產生的變化,我們需要通過計算mod的尤拉值phi,計算了幾次就需要取幾次mod的尤拉值,即phi(phi(mod))...

       這些可以通過dfs來實現,從字串最後一項向前dfs過程中,遇到2的時候將mod修改為當前mod的尤拉值,對於dfs,他會先計算字串首的時間返回給後面,從而最終是從首項開始計算時間 t .

       還有需要注意的是需要提前預處理出mod的尤拉值phi(mod),和phi(mod)的尤拉值phi(phi(mod))........以便之後使用時接近O(1)的時間內在map中查詢得到結果,聽說不弄會TLE.

程式碼:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
map<ll, ll>phi;                //phi[x]=x的尤拉值
char s[maxn];
ll eular(ll n) {               //log(n)時間內求一個數的尤拉值
	ll ans = n; 
	for (ll i = 2; i*i <= n; i++) {
		if (n%i == 0)
		{
			ans -= ans / i;
			while (n%i == 0) n /= i;
		}
	}
	if (n > 1) ans -= ans / n;
	return ans;
}
void fun() {                   //預處理出mod=1e9+7的所有phi,eg:phi(mod),phi(phi(mod))...
	ll x = mod;
	while (x!=1) {
		phi[x] = eular(x);
		x = phi[x];
	}
	phi[1] = 1;
}
ll mod_pow(ll a, ll n,ll MOD) {//快速冪取膜
	a %= MOD;
	ll ret = 1;
	while (n) {
		if (n & 1) ret = (ret*a) % MOD;
		a = a*a%MOD;
		n >>= 1;
	}
	return ret;
}
ll dfs(int x, ll MOD) {        //從後往前dfs從而先計算前面的
	if (x == 0) return 0;
	if (s[x] == '0') return (dfs(x - 1, MOD) + 1)%MOD;
	else if (s[x] == '1')return ((ll)2 * dfs(x - 1, MOD) + (ll)2) % MOD;
	else return (((ll)3 * mod_pow(2, dfs(x - 1, phi[MOD])+1, MOD) - (ll)3) % MOD+MOD)%MOD;
}
int main()
{
	fun();
	int t;
	scanf("%d", &t);
	while (t--) {
		cin >> s + 1;
		int len = strlen(s+1);
		printf("%lld\n", dfs(len,mod));
	}
	return 0;
}

這道題可以再好好研究。

打表程式碼:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s,h;
    long long num;
    while(cin>>s)
    {
        num=0;
        while(!s.empty())
        {
            h="";
            for(int i=0;i<s.length();i++)
            {
                h=h+s[i];
              ///  cout<<h<<"h"<<endl;
                if(s[i]=='2')
                {
                    h=h+'1';
                }
                else if(s[i]=='1')
                {
                    h=h+'0';
                }
            }
            h=h.substr(1,h.length());
            s=h;
            cout<<s<<endl;
            num++;
        }
        cout<<num<<endl;
    }
}