打表找規律,推出了公式,但是各種調試還是不過
最後發現在公式裡有一個除法,而對相應的模數操作忘處理了,導致一直WA到比賽結束都沒調出來
以後要注意這個問題了。。。
這題類似高中的排列組合裡的傳球問題, 甲乙丙丁4人傳球,從甲傳n次到甲 和從甲傳n次到乙丙丁的個數即為所要求的分段的個數。
能夠推出公式分別自己到自己和自己到其他人2種 , 分奇偶2種公式
if ( Node[i].s == Node[i-1].s ) { temp = ((3*(cal_3( t ) +(t&1?1:(-1))) )%mod)/4; //這裡的除法一開始沒注意,凡涉及和除法有關的mod都要處理下,要記住這次教訓 ans = ans * temp%mod_; } else if ( Node[i].s != Node[i-1].s) { temp = (((cal_3( t+1 ) -(t&1?1:(-1))) )%mod)/4; ans = ans * temp%mod_; }
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;typedef unsigned long long ll;const ll mod=4*1000000007ll;const ll mod_=1000000007ll;ll pow3[100];//3^(2^i);long long cal_3( int n ){ //cout << n << endl; ll res=1; for (int t=0 ; n ; n>>=1 ,t++ ) if(n&1)res=res*pow3[t]%mod; //cout << res << endl; return res;}struct node {char s ;int loc;}Node[15];bool cmp ( node a, node b ){ return a.loc < b. loc;}int main(){ int n , m; char c; /// pow3[0]=3; for (int i=0 ; i<50 ;++i) { pow3[i+1]=pow3[i]*pow3[i]%mod; //printf("%lld %lld\n",pow3[i] * pow3[i] ,pow3[i+1]); } /// //freopen ("in.txt" , "r" , stdin); //freopen ("out.txt" , "w" ,stdout ); while (scanf("%d%d",&n , &m ) != EOF ) { if(m==0){cout <<(4*cal_3(n-1)%mod_) <<endl;continue;} for ( int i = 0 ; i < m ; i ++ ) { scanf("%d%c%c" ,&Node[i].loc, &c ,&Node[i].s ); } long long ans = 1; sort ( Node , Node + m , cmp ); ans = ans*cal_3(Node[0].loc-1 )%mod_; //cout << ans << endl; for ( int i = 1 ; i < m ; i ++ ) { int t = (Node[i].loc - Node[i-1].loc -1 ); long long temp; if ( Node[i].s == Node[i-1].s ) { temp = ((3*(cal_3( t ) +(t&1?1:(-1))) )%mod)/4; //這裡的除法一開始沒注意,凡涉及和除法有關的mod都要處理下,要記住這次教訓 ans = ans * temp%mod_; } else if ( Node[i].s != Node[i-1].s) { temp = (((cal_3( t+1 ) -(t&1?1:(-1))) )%mod)/4; ans = ans * temp%mod_; } } ans = ans*cal_3( n - Node[m-1].loc )%mod_; //printf("%d\n", ans%mod ); cout << (ans % mod_ ) << endl; } return 0;}