浙大月赛的题... 比赛的时候一直都没过 找的规律 后来生成的数据 与姜教主将标程和自己的程序一个一个对的..
发现是除数取模的时候的问题 .. 以后再也不能犯这样的错误了..
姜教主的代码:
#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;
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;
}
然后标程是用矩阵乘法做的 大概看了一下:意思就是用4×4的矩阵 矩阵乘法模拟状态的转移..都是Logn级别的算法.. 思路很好 还不容易出错。
代码如下:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
#define PB push_back
#define MP make_pair
int N = 4;
const int MOD = 1000000007;
struct mat{
long long data[4][4];
}b, c;
void mul(mat& c, const mat& a, const mat& b){
int i, j, k;
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
for(c.data[i][j] = k = 0; k < N; k++){
c.data[i][j] = (c.data[i][j] + a.data[i][k] * b.data[k][j]) % MOD;
}
}
}
}
void gao(mat& c, const mat& a, int l){
int i, j, k;
mat e, f;
if(l == 1){
c = a;
return;
}
k = l % 2;
int u;
u = l / 2;
if(k){
gao(e, a, u);
mul(f, e, e);
mul(c, f, a);
}else{
gao(f, a, u);
mul(c, f, f);
}
return;
}
pair<int, int> a[12];
int main(){
int i, j, k, m, n;
char s[10];
while(scanf("%d%d", &n, &m) != EOF){
assert(1 <= n && n <= 10000000);
assert(0 <= m && m <= 10);
long long ans = 1, t = 0;
for(i = 0; i < m; i++){
scanf("%d%s", &a[i].first, s);
assert(1 <= a[i].first && a[i].first <= n);
assert(s[1] == 0 && 'A' <= s[0] && s[i] <= 'D');
for(j = 0; j < i; j++) assert(a[i].first != a[j].first);
a[i].second = s[0] - 'A';
}
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
b.data[i][j] = (i == j) ? 0 : 1;
if(m == 0){
if(n > 1){
gao(c, b, n - 1);
t = 0;
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
t = (t + c.data[i][j]) % MOD;
printf("%lld\n", t);
}else{
puts("4");
}
continue;
}
sort(a, a + m);
if(a[0].first > 1){
gao(c, b, a[0].first - 1);
t = 0;
for(j = 0; j < 4; j++){
t = (t + c.data[j][a[0].second]) % MOD;
}
ans = (ans * t) % MOD;
}
for(i = 1; i < m; i++){
gao(c, b, a[i].first - a[i - 1].first);
ans = (ans * c.data[a[i].second][a[i - 1].second]) % MOD;
}
if(a[m - 1].first < n){
gao(c, b, n - a[m - 1].first);
t = 0;
for(j = 0; j < 4; j++)
t = (t + c.data[a[m - 1].second][j]) % MOD;
ans = (ans * t) % MOD;
}
printf("%lld\n", ans);
}
return 0;
}