牛客暑假多校——A-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的状况显然是不存在的。

前提:之前已已经操作了 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;
}

ps:本篇参考:https://blog.csdn.net/weixin_41156591/article/details/81267119

这道题可以再好好研究。

打表代码:

#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;
    }
}

 

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值