比赛被虐了,一个都不会,这题只要是找出规律,然后要注意的的mod,MOD要用long long 这坑了好久
/*
** 时间:2014-9-29
** 知识点:
** 题意:F(n) = 3/4 * (3^L - (-1)^L) ; D(n) = (3^(L+1) + (-1)^L) / 4 ;
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const ll mod=4*1000000007ll;
const ll MOD=1000000007ll;
int n,m;
struct node{
int pos;
char c;
}g[20];
bool cmp(node a,node b)
{
return a.pos<b.pos;
}
long long pow_3(int a)
{
long long res=1;
while(a)
{
//cout<<a<<endl;
a--;
res*=3;
res%=mod;
int b=1;
long long s=3;
while(a-b>=0)
{
res*=s;
res%=mod;
s*=s;
s%=mod;
a-=b;
b<<=1;
}
}
return res;
}
void solve(){
long long ans=1,num;
if(m==0)
{
ans=4*pow_3(n-1)%mod;
cout<<ans%MOD<<endl;return;
}else{
sort(g,g+m,cmp);
ans=pow_3(g[0].pos-1)%mod;
ans=(ans*pow_3(n-g[m-1].pos))%mod;
for(int i=1;i<m;i++)
{
int l=g[i].pos-g[i-1].pos-1;
if(g[i].c!=g[i-1].c){
//(3^(L+1) + (-1)^L) / 4
num = (((pow_3( l+1 ) -(l&1?1:(-1))) )%mod)/4;
ans = ans * num % mod;
}else if(g[i].c==g[i-1].c){
// 3/4 * (3^L - (-1)^L)
num = ((3*(pow_3( l ) +(l&1?1:(-1))) )%mod)/4; ;
ans = ans * num % mod;
}
}
cout<<ans%MOD<<endl;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0; i<m; i++)
{
cin>>g[i].pos>>g[i].c;
}
solve();
}
return 0;
}