zoukankan      html  css  js  c++  java
  • 【dp】Arrange the Schedule

    题目链接: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)。

     1 /***Good Luck***/
     2 #define _CRT_SECURE_NO_WARNINGS
     3 #include <iostream>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <string>
     8 #include <algorithm>
     9 #include <stack>
    10 #include <map>
    11 #include <queue>
    12 #include <vector>
    13 #include <set>
    14 #include <functional>
    15 #include <cmath>
    16 #include <numeric>
    17 
    18 #define Zero(a) memset(a, 0, sizeof(a))
    19 #define Neg(a)  memset(a, -1, sizeof(a))
    20 #define All(a) a.begin(), a.end()
    21 #define PB push_back
    22 #define inf 0x3f3f3f3f
    23 #define inf2 0x7fffffffffffffff
    24 #define ll long long
    25 using namespace std;
    26 //#pragma comment(linker, "/STACK:102400000,102400000")
    27 void get_val(int &a) {
    28     int value = 0, s = 1;
    29     char c;
    30     while ((c = getchar()) == ' ' || c == '
    ');
    31     if (c == '-') s = -s; else value = c - 48;
    32     while ((c = getchar()) >= '0' && c <= '9')
    33         value = value * 10 + c - 48;
    34     a = s * value;
    35 }
    36 const int maxn = 14;
    37 pair<char, int> pr[maxn];
    38 int n, m;
    39 ll mod = 1000000007;
    40 inline ll modxp(ll a, ll b, ll mod) {
    41     ll ret = 1;
    42     ll tmp = a;
    43 
    44     while (b) {
    45         if (b & 1) ret = ret* tmp %mod;
    46         tmp = tmp * tmp % mod;
    47         b >>= 1;
    48     }
    49     return ret;
    50 }
    51 
    52 bool cmp(pair<char, int> a, pair<char, int> b) {
    53     return a.second < b.second;
    54 }
    55 int main() {
    56     //freopen("data.out", "w", stdout);
    57     //freopen("data.in", "r", stdin);
    58     //cin.sync_with_stdio(false);
    59     while (cin >> n >> m) {
    60         ll ans = 1;
    61         if (m == 0) {
    62             cout << (4 * modxp(3, n - 1, mod)) % mod << endl;
    63             continue;
    64         }
    65         for (int i = 1; i <= m; ++i) {
    66             cin >> pr[i].second >> pr[i].first;
    67         }
    68         sort(pr + 1, pr + m + 1, cmp);
    69         ans *= modxp(3, pr[1].second - 1, mod);
    70         ans *= modxp(3, n - pr[m].second, mod);
    71         ans %= mod;
    72         ll t;
    73         bool flag = false;
    74         for (int i = 2; i <= m; ++i) {
    75             if (pr[i].first == pr[i - 1].first && pr[i].second - pr[i - 1].second == 1) {
    76                 cout << 0 << endl;
    77                 flag = true;
    78                 break;
    79             }
    80             if (pr[i].second - pr[i - 1].second == 1) continue;
    81 
    82             else {
    83                 t = pr[i].second - pr[i - 1].second;
    84                 if (pr[i].first == pr[i - 1].first) {
    85                     ans *= ((3 * ((t & 1) ? -1 : 1) + modxp(3, t, mod))  * 250000002) % mod ;
    86                     ans %= mod;
    87                 }
    88                 else {
    89                     ans *= ((((t & 1) ? 1 : -1) + modxp(3, t, mod)) * 250000002) % mod;
    90                     ans %= mod;
    91                 }
    92             }
    93         }
    94         if (!flag) 
    95         cout << ans << endl;
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    mysql对库,表,数据类型的操作以及完整性约束
    mysql数据库初步了解
    响应式及Bootstrap
    事件流丶事件对象
    JQuery初识(三 )
    JQuery初识(二)
    JQuery初识
    sencha touch tpl 实现按钮功能
    sencha touch 分享到微博扩展
    sencha touch 隐藏滚动条样式的几种方式
  • 原文地址:https://www.cnblogs.com/yeahpeng/p/3993426.html
Copyright © 2011-2022 走看看