本文主要是介绍牛客多校 Ternary String (数论),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
链接: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.
示例1
输入
复制
3
000
012
22
输出
复制
3
93
45
思路:
1. 如果遇到了 0, t++
2. 如果遇到了1,t = t*2+2
因为之前经历了t,那么就会派生出来t个0,加上一个1的时间 2 就是上式了
3. 如果遇到了2,t = 3*(2^t -1)
可以类似于2来想,经过了t后到了一个2
会变成这样 21101001000100001.......
再过一秒会变成1101001000100001000001....... // **** 等了一秒
观察这个序列可以发现:该序列满足2的结论 。
消灭到第一个1和他后面的0 需要 a1 = 2
消灭到第二个1和他后面的0 需要 a2 = 2*a1+2 + 2-1 = 7
消灭到第三个1和他后面的0 需要 a3 = 2*a2+2 + 3-1 = 18
消灭到第 i 个1和他后面的0 需要 a[i] = 2*a[i-1]+2+i-1 = 2*a[i-1]+i+1
有了 a[i+1] = 2*a[i]+i+2,a[1] = 2, 就可以求a[n] 了
两边同时加 i+4 得到 a[i+1]+(i+1)+3 = 2(a[i]+i+3)
令b[n] = a[n]+n+3 , b1 = a1+4 = 6;
b[n+1]/b[n] = 2;
得到 b[n] = 6*2^(n-1) = 3*2^n
得到 a[n] = b[n]-n-3 = 3*2^n-n-3
a[n] 就是在之前时间为n-1 (因为之前等了一秒)时,当前2消掉的花费了。
再加上之前的花费n-1 + 1可以算出总的当前花费 3*2^n-3
那么由于每次都要对前面的结果取一次指数,就得用指数循环节了
1e9+7 开始打个表,20多次phi就能打到1了·剩下的就是维护指数,和结果了
倒存一下mod,正向一遍出结果。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
char s[maxn];
stack <ll> mod;
ll qm(ll a, ll n, ll m)
{if(a >= m) a = a%m+m;ll ans = 1;while(n){if(n%2) {ans = ans*a;if(ans >= m) ans = ans%m+m; } a = a*a;if(a >= m) a= a%m+m;n/=2;}return ans;
}
map <int,ll> phi;
ll get_phi(ll n)
{ll ans = n;for(ll i = 2; i*i <= n; i++){if(n%i == 0) ans = ans-ans/i;while(n%i==0) n/=i;}if(n!=1) ans = ans-ans/n;return ans;
}int main()
{ll kk;phi[1] = 1;for(ll i = 1e9+7; i > 1; i=kk){kk = get_phi(i);phi[i] = kk;}int t;scanf("%d",&t);while(t--){scanf("%s",s);int len = strlen(s);while(mod.size()) mod.pop();ll md = 1e9+7;for(int i = len-1; i >= 0; i--){if(s[i] == '2') {mod.push(md);md = phi[md];}}ll ans = 0;ll tmp = 0;ll MOD = 1e9+7;if(mod.empty()) mod.push(MOD);for(int i = 0; s[i]; i++){if(s[i] == '0'){ans=(ans+1)%MOD;tmp=(tmp+1);if(tmp >= md) tmp = tmp%md+md;}if(s[i] == '1'){ans=(2LL*ans+2LL)%MOD;tmp=(2LL*tmp+2LL);if(tmp >= md) tmp = tmp%md+md;}if(s[i] == '2') {md = mod.top();mod.pop();tmp = 3LL*qm(2,tmp+1,md);ans = (tmp-3+MOD)%MOD;tmp = (tmp-3+md*MOD);if(tmp >= md) tmp = tmp%md+md;}}printf("%lld\n",ans);}return 0;
}
这篇关于牛客多校 Ternary String (数论)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!