题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3538

题意:如题。

题解: 假如 一组数据 。。。(n1)A。。。。(n2)A。。。。(n2)   由于三部分为独立事件, 则总数为三部分相乘。 (1)(3)易求,即3^n1, 3^n3,对于 (2), 当头尾相同(如样例) 可推出 an = 2an - 1 + 3an - 2; (a1 = 0, a2 = 3) 则 an =( 3(-1)^(n - 2) + 3^n ) / 4; 当头尾不想同(如 A。。。。B)an = 2an - 1 + 3an - 2;(a1 = 1; a2 = 2) 则an = ((-1)^(n - 1) + 3^n)/4(计算是4 取逆元);

需要注意 当m = 0的数据, 以及本身相邻两个字母相同(结果为0)。

 /***Good Luck***/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <functional>
#include <cmath>
#include <numeric> #define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a) memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define inf 0x3f3f3f3f
#define inf2 0x7fffffffffffffff
#define ll long long
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
void get_val(int &a) {
int value = , s = ;
char c;
while ((c = getchar()) == ' ' || c == '\n');
if (c == '-') s = -s; else value = c - ;
while ((c = getchar()) >= '' && c <= '')
value = value * + c - ;
a = s * value;
}
const int maxn = ;
pair<char, int> pr[maxn];
int n, m;
ll mod = ;
inline ll modxp(ll a, ll b, ll mod) {
ll ret = ;
ll tmp = a; while (b) {
if (b & ) ret = ret* tmp %mod;
tmp = tmp * tmp % mod;
b >>= ;
}
return ret;
} bool cmp(pair<char, int> a, pair<char, int> b) {
return a.second < b.second;
}
int main() {
//freopen("data.out", "w", stdout);
//freopen("data.in", "r", stdin);
//cin.sync_with_stdio(false);
while (cin >> n >> m) {
ll ans = ;
if (m == ) {
cout << ( * modxp(, n - , mod)) % mod << endl;
continue;
}
for (int i = ; i <= m; ++i) {
cin >> pr[i].second >> pr[i].first;
}
sort(pr + , pr + m + , cmp);
ans *= modxp(, pr[].second - , mod);
ans *= modxp(, n - pr[m].second, mod);
ans %= mod;
ll t;
bool flag = false;
for (int i = ; i <= m; ++i) {
if (pr[i].first == pr[i - ].first && pr[i].second - pr[i - ].second == ) {
cout << << endl;
flag = true;
break;
}
if (pr[i].second - pr[i - ].second == ) continue; else {
t = pr[i].second - pr[i - ].second;
if (pr[i].first == pr[i - ].first) {
ans *= (( * ((t & ) ? - : ) + modxp(, t, mod)) * ) % mod ;
ans %= mod;
}
else {
ans *= ((((t & ) ? : -) + modxp(, t, mod)) * ) % mod;
ans %= mod;
}
}
}
if (!flag)
cout << ans << endl;
}
return ;
}
05-26 05:19