[DP] arrange the schedule

Source: Internet
Author: User

Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3538.

Question: for example, question.

Question: assume a group of data... (N1) .... (N2) .... (N2) because the three parts are independent events, the total number is multiplied by the three parts. (1) (3) easy to find, that is, 3 ^ N1, 3 ^ N3. For (2), when the head and tail are the same (as in the example) an = 2an-1 + 3an-2 can be introduced; (a1 = 0, a2 = 3) then an = (3 (-1) ^ (n-2) + 3 ^ N)/4; when the head and tail do not want the same (such as .... B) An = 2an-1 + 3an-2; (a1 = 1; a2 = 2) then an = (-1) ^ (n-1) + 3 ^ N) /4 (The calculation is 4 to retrieve the reverse element );

 

Note that when m = 0, and the adjacent two letters are the same (the result is 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_back22 #define inf 0x3f3f3f3f23 #define inf2 0x7fffffffffffffff24 #define ll long long25 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 == ‘\n‘);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 }

 

[DP] arrange the schedule

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.