博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
牛客网第4场A
阅读量:4309 次
发布时间:2019-06-06

本文共 2207 字,大约阅读时间需要 7 分钟。

链接: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输入复制300001222输出复制39345

欧拉降幂且记录;

(1)如果在消除一个 0 前经过了 n 秒,那么消掉这个 0 需要 n + 1 秒。

(2)如果在消除一个 1 前经过了 n 秒,那么消掉这个 1 与其产生的所有数需要 (n + 1) * 2 秒。

(3)如果在消除一个 2 前经过了 n 秒,那么消掉这个 2 与其产生的所有数需要 (2 ^ (n + 1) - 1) * 3 秒。

 

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define ll long longmap
mp;char s[100000];ll phi(ll x){ if(mp[x]) return mp[x]; ll temp=x; ll ans=x; if(x==1) return mp[1]=1; for(ll i=2;i*i<=x;i++){ if(x%i==0){ ans=ans/i*(i-1); while(x%i==0) x/=i; } } if(x>1) ans=ans/x*(x-1); return mp[temp]=ans;} ll qsm(ll a,ll b,ll c){ ll ret = 1; for (;b;b >>= 1,(a *= a)%=c) if (b & 1) (ret *= a)%=c; return ret;}ll solve(ll x,ll mod){ if(x==0||mod==1) return 0; if(s[x]=='0'){ return (1LL+solve(x-1,mod)+mod)%mod; }else if(s[x]=='1'){ return (2*(solve(x-1,mod)+1)+mod)%mod; }else{ ll ph=phi(mod); ll t=(solve(x-1,ph))%ph; return (qsm(2,t,mod)*6%mod-3+mod*3)%mod; }}int main(){ int n; scanf("%d",&n); while(n--) { scanf("%s",s+1); printf("%lld\n",solve(strlen(s+1),1e9+7)); } return 0;}

 

 

转载于:https://www.cnblogs.com/2014slx/p/9389594.html

你可能感兴趣的文章
力扣题解-230. 二叉搜索树中第K小的元素(递归方法,中序遍历解决)
查看>>
力扣题解-123. 买卖股票的最佳时机 III(动态规划)
查看>>
Django 源码阅读:服务启动(wsgi)
查看>>
Django 源码阅读:url解析
查看>>
Docker面试题(一)
查看>>
第一轮面试题
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
一、redis面试题及答案
查看>>
消息队列2
查看>>
C++ 线程同步之临界区CRITICAL_SECTION
查看>>
测试—自定义消息处理
查看>>
MFC中关于虚函数的一些问题
查看>>
根据图层名获取图层和图层序号
查看>>
规范性附录 属性值代码
查看>>
提取面狭长角
查看>>
Arcsde表空间自动增长
查看>>
Arcsde报ora-29861: 域索引标记为loading/failed/unusable错误
查看>>
记一次断电恢复ORA-01033错误
查看>>
C#修改JPG图片EXIF信息中的GPS信息
查看>>