题目链接: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 ;
}

【dp】Arrange the Schedule的更多相关文章

  1. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  4. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. Linux本地内核提权漏洞复现(CVE-2019-13272)

    Linux本地内核提权漏洞复现(CVE-2019-13272) 一.漏洞描述 当调用PTRACE_TRACEME时,ptrace_link函数将获得对父进程凭据的RCU引用,然后将该指针指向get_c ...

  2. LeetCode108——Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  3. 百万年薪python之路 -- 面试之葵花宝典

    关于for面试题: for i in "alex": pass print(i) 结果: x 关于字符串的面试题: s = "给章超印倒一杯卡布奇洛" s[:: ...

  4. VirtualBox NAT Network配置

    VirtualBox NAT Network配置(OSX上的) VirtualBox的5种连接方式 NAT :虚拟机之间不能互通 NAT网络 :本文对象 桥接 :一般情况下虚拟机无法设置静态IP,并且 ...

  5. ajax同步请求与异步请求的区别

    ajax 区别: async:布尔值,用来说明请求是否为异步模式.async是很重要的,因为它是用来控制JavaScript如何执行该请求. 当设置为true时,将以异步模式发送该请求,JavaScr ...

  6. 微信支付 get_brand_wcpay_request fail,Undefined variable: openid

    本文将为您描述微信H5支付,微信JSAPI支付返回支付签名验证失败的解决方法 微信JSAPI支付时报这个错误 查看错误详情 alert(JSON.stringify(res)) 微信商户平台相关设置: ...

  7. markdown语法(转)

    markdown语法 1.标题代码 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 2.引用代码 >你好 >> 您好 ...

  8. Java多线程编程(七)线程状态、线程组与异常处理

    一.线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在于State枚举类中. 调用与线程有关的方法后,会进入不同的线程状态,这些状态之间某些是可双向切换的,比如WAITING和RUNNI ...

  9. any_value()函数

    转载自:https://blog.csdn.net/Peacock__/article/details/90608246 MySQL5.7之后,sql_mode中ONLY_FULL_GROUP_BY模 ...

  10. 一个普通类就能干趴你的springboot,你信吗?

    先声明本人并不是标题党,如果看了本篇文章并且认为没有得到任何收获,请您随便留言骂我,本人绝不还口,已经对springboot了如指掌大大神,求放过! 不BB了,直接上代码,请各位在自己的springb ...