栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3538 Arrange the Schedule

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3538 Arrange the Schedule

#include<stdio.h>#include<algorithm>#include<memory.h>#define LL long long#define M 1000000007using namespace std;struct point {int a;char c;} p[20];char s[100];struct maze {LL s[3][3];void unit() {int i;memset(s, 0, sizeof(s));for (i = 0; i < 3; i++)s[i][i] = 1;}void out() {int i, j;for (i = 0; i < 3; i++) {for (j = 0; j < 3; j++)printf("%lld ", s[i][j]);printf("n");}return;}} c, b[2], d;LL ans;LL a[3];int n, m, i, l, f, g, op, j, nn;LL pow(int k, int n) {LL ret = 1, p = k;if (n == 0)return 1;while (n) {if (n & 1)ret = ret * p % M;p = p * p % M;n >>= 1;}return ret;}maze mul(maze a, maze b) {int i, j, k;maze ret;memset(ret.s, 0, sizeof(ret));for (k = 0; k < 3; k++)for (i = 0; i < 3; i++)for (j = 0; j < 3; j++) {ret.s[i][j] = ((ret.s[i][j] + a.s[i][k] * b.s[k][j]) % M);if (ret.s[i][j] >= M)ret.s[i][j] -= M;}return ret;}bool cmp(point x, point y) {return x.a < y.a;}int main() {b[0].s[0][0] = 2;b[0].s[0][1] = 2;b[0].s[0][2] = 0;b[0].s[1][0] = 1;b[0].s[1][1] = 1;b[0].s[1][2] = 0;b[0].s[2][0] = 0;b[0].s[2][1] = 1;b[0].s[2][2] = 1;b[1].s[0][0] = 2;b[1].s[0][1] = 2;b[1].s[0][2] = 0;b[1].s[1][0] = 1;b[1].s[1][1] = 1;b[1].s[1][2] = 0;b[1].s[2][0] = 0;b[1].s[2][1] = -1;b[1].s[2][2] = 1;c = mul(b[0], b[1]);while (scanf("%d%d", &n, &m) != EOF) {for (i = 1; i <= m; i++) {scanf("%d %s", &p[i].a, s);p[i].c = s[0];}if (!m) {ans = 4 * pow(3, n - 1) % M;printf("%lldn", ans);continue;}sort(p + 1, p + 1 + m, cmp);ans = pow(3, p[1].a - 1);ans = (ans * pow(3, n - p[m].a)) % M;bool boo = true;for (i = 1; i < m; i++)if (p[i].a + 1 == p[i + 1].a && p[i].c == p[i + 1].c) {boo = false;}if (!boo) {printf("0n");continue;}for (j = 1; j < m; j++) {if (p[j + 1].a == p[j].a + 1)continue;l = p[j + 1].a - p[j].a - 1;a[0] = 1;a[1] = 0;a[2] = 1;nn = l / 2;maze res, d = c;res.unit();while (nn) {if (nn & 1)res = mul(res, d);d = mul(d, d);nn >>= 1;}if (l & 1)res = mul(res, b[0]);if (p[j + 1].c != p[j].c) {ans = ans* (a[0] * res.s[0][0] % M + a[1] * res.s[1][0] % M+ a[2] * res.s[2][0] % M) % M;} else {ans = ans* (a[0] * res.s[0][1] % M + a[1] * res.s[1][1] % M+ a[2] * res.s[2][1] % M) % M;}}printf("%lldn", ans);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号