1. 程式人生 > >牛客多校第四場A ternary string ----推公式和指數迴圈節

牛客多校第四場A ternary string ----推公式和指數迴圈節

Ternary String
時間限制:C/C++ 4秒,其他語言8秒
空間限制:C/C++ 131072K,其他語言262144K
64bit IO Format: %lld
題目描述
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,直接刪除,操作次數+1
2、遇到1,考慮之前已經操作了x次,那麼這個1後面已經多生出了x個0,這個時候需要在經過x + 2次操作才能刪完
3、遇到2,考慮之前已經操作了x次,然後打個表可以發現,之後還需要經過3 * (2^(x + 1)- 1 ) - x 次操作才能全部刪完。

可見計算a*2 ^ x mod (10 ^ 9 + 7),於是也得計算x mod phi(10 ^ 9 + 7),考慮到x可能也是之前某些2^x的組合,因此還得算x mod phi(phi(10 ^ 9 + 7)),以此類推
另外考慮到若干次後,mod一定是一個2的倍數,可以提前算好答案,來優化。
嗨,主要是推公式很難推出來!!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 100005;
char s[N];
LL phi[35]={1000000007,1000000006,500000002,//3
243900800,79872000,19660800,5242880,2097152,//5
1048576,524288,262144,131072,65536,32768,16384,//7
8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1,1};//13
LL pow_mod(LL n,LL m,LL p)
{
    LL x = n;
    LL sum = 1;
    while(m)
    {
        if(m & 1)
            sum = (sum * x) % p;
        m >>= 1;
        x = (x * x) % p;
    }
    return sum;
}
LL dfs(int pos,int num)
{
    if(num > 27) return 0;
    if(pos == -1) return 0;
    if(s[pos] == '0'){
        return (dfs(pos - 1,num) + 1LL) % phi[num];
    }
    else if(s[pos] == '1'){
        return (dfs(pos - 1,num) * 2LL + 2LL) % phi[num];
    }else{
        //後面加+phi[num]) % phi[num]是為了防止出現負數 
        return (((3LL * pow_mod(2,dfs(pos - 1,num + 1) + 1,phi[num])) - 3LL) % phi[num] + phi[num]) % phi[num];
    }
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%s",s);
            int len = strlen(s);
            printf("%lld\n",dfs(len - 1,0));
        }
    }
    return 0;
}

相關推薦

A ternary string ----公式指數迴圈

Ternary String 時間限制:C/C++ 4秒,其他語言8秒 空間限制:C/C++ 131072K,其他語言262144K 64bit IO Format: %lld 題目描述 A ternary string is a sequenc

2018 A Ternary String

設dp[i]為刪掉原本的第i位需要經過多少天,然後可以根據規則發現一些規律,如果第i位是2的話,dp[i]=6*2^dp[i-1]-3,如果是第1位的話就是dp[i]=2^dp[i-1]+2,0:dp[i]=dp[i-1]+1,然而dp陣列是非常大的,需要對mod進行取模,但

G Maximum Mode

clas pear contain 記錄 The 但是 -m out appear 鏈接:https://www.nowcoder.com/acm/contest/142/G來源:牛客網 The mode of an integer sequence is the valu

J Hash Function(拓撲排序 + 線段樹建邊)

題目描述 Chiaki has just learned hash in today's lesson. A hash function is any function that can be used to map data of arbitrary size to

A(01分數規劃)

時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 262144K,其他語言524288K Special Judge, 64bit IO Format: %lld 題目描述 Kanade selected n courses in the univers

G Maximum Mode(模擬)

題目大意: 給出一個n個數的序列,我們可以刪除m個數,然後我們要求出現次數最多並且最大的, 也就是如果出現次數最多的有多個,那就必須刪除其他的數,避免出現次數最大的有多個,然後我們要求值最大,所以我們還要儘量判斷值大的  題目思路: 首先我們先明確我們要取什麼數:

E Sort String(字串hash)

題目描述 Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and won

-D-inv

item return text 因此 .... src acm ... ati 鏈接:https://www.nowcoder.com/acm/contest/143/D來源:牛客網 題目描述 Kanade has an even number n and a

2018 H.subseq

print int space 跳出循環 date open 遞增 code play 題意:   給出a數組的排列。求出字典序第k小的b數組的排列,滿足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0。 題解:  

2018E(動態規劃,思維,取模)

pac namespace for ons mod 思維 space scan 動態規劃 #include<bits/stdc++.h>using namespace std;const long long mod=1000000007,inv=57000000

C

++ 預處理 ima class -s inf using com false 一個數很大,並不能預處理,所以要進行公式變換,存前一個的值就好 #include <bits/stdc++.h> using namespace std; typede

F - take (樹狀陣列)

題目連結 初始的時候手裡有大小為0的鑽石,現在依次開啟n個箱子,第i個箱子有x/100的概率開出大小為y的鑽石。如果開出的鑽石比手中的要大,就交換一次。求交換次數的期望。 考慮要在某個位置交換,就必須滿足在它前面的比它大的鑽石都沒有被開出來。 也就是,第i個位置對答案的貢獻是。 對於

2018賽第一 A Monotonic Matrix(組合計數)

大致題意:給你一個n*m的矩陣,每一個位置可以填0、1和2三個數字,但是要求每個數字下面和右邊的數字要大於等於他自己。現在問滿足條件的填數字方法有多少種。 看完這道題,很明顯的一個dp題。轉移方程也很容易想出來,但是這樣寫出來你會發現樣例也過不了-_-。

F Sum Of Digit(線段樹區間合併)

題目描述 Eddy likes to play with digits. However, as you may know, Eddy is a programmer not a normal human. Thus, he likes to play with hex

F take(期望+線段樹)

題目描述 Kanade has n boxes , the i-th box has p[i] probability to have an diamond of d[i] size. At the beginning , Kanade has a diamond o

3 B Expected Number of Nodes 題解

題意: 就是給你一個壓縮圖的方式,然後讓你計算任意選擇k(1….n)個點之後的圖上剩餘點的期望數(mod 1e9+7). 做法: 很顯然的得到兩個結論(記當前要保留的點為k,總點數為n). 1.對於一個度數小於等於2的點,他的留下來的概率

9H Prefix Sum

這題正解是線段樹上打等差數列的tag,然後維護。不過我看不太懂= =,隊友想起了盧總講過的big-small分塊暴力思想,對於一個x位置加了一個y,a[k][idx]就加了C(idx+k-x-1,k-1),我們開一個佇列長度為sz儲存一蛤操作,超過sz就把佇列中的+操作放進a

2019

H Cutting Bamboos 題意 給n個高度,每次獨立詢問一個區間\([l,r]\),對於這個區間的所有高度,要求砍\(y\)次剛好全部砍完,問第\(x\)次砍的位置。 分析 可以二分砍的位置,計算出第\(x\)次砍掉的所有高度,進行check,所以問題就轉化為如何求區間\([l,r]\)裡大於某個

2020K題

# __int128(例題:2020牛客多校第八場K題) ## 題意: > 有n道菜,第i道菜的利潤為$a_i$,且有$b_i$盤。你要按照下列要求給顧客上菜。 >1.每位顧客至少有一道菜 > 2.給顧客上菜時,都必須從第一道菜開始,上連續的編號的菜,例如,你可能給一位顧客 上的菜為第一道,第二道,第三道

杭電 A Ascending Rating

it is xor esc pri main param ack led preview Problem Description Before the start of contest, there are n ICPC contestants waiting in a l