SARS virus (power generation function + fast)

 
     

Links: https://ac.nowcoder.com/acm/contest/992/A
Source: Cattle-off network

 
     

Title Description

 
     

At present, the SARS virus in the world, discovered by scientists, a single strand of the virus and its variants of DNA, cytosine, thymine are paired. Although this is a major discovery, but not the most important feature of the virus, because this feature is too weak.

In order to find out the characteristics of the virus, CN Centers for Disease Control and Alibaba Group cooperation, with the power of thinking and procedures of science and technology to solve this problem. Alibaba now appoint you a special SARS senior fellow at the CN Center for Disease Control to study in this feature may be the number of DNA sequence of the SARS virus. More precisely, you need to count all of the following conditions is the number of length n string:

  1. Only strings of A, T, C, G composition
  2. A occurs even number of times (or may not occur)
  3. C occurs even number of times (or may not appear)

When n = 2, all the following conditions are satisfied string 6:

TT,TG,GT,GG,AA,CC。

Note: Because this number can be very large, you can just give the results of the 10 ^ 9 + 7 modulo.

Enter a description:

Multiple sets of input (not more than 10 groups), each line an integer n: 0 <n <10 ^ 10 ^ 5

Output Description:

To 10 ^ 9 +7 result modulo the number of input files for each of n, satisfying the condition of the output string.
Example 1

Entry

1
2
100

Export

2
6
113046907

Analysis: First, the generating function of a subject, can know from ACGT, AC can only occur even number, odd and GT can be even, generating function is listed 
in front of that is AC, and behind that is a GT, after this listed, as long as it is calculated that one factor is the answer.
First of all the direct demand is certainly not out of seeking to use the Taylor expansion formula requirements.
 
The above two equations can be simplified to the generating function, the result is simplified
further Taylor expansion according to the formula, to obtain the coefficient of the item.
The end result is , the result looks very simple ah, but n is too great ah, so it needs to be processed n.
Hypothesis 2 n-  result, the first is to 1e9 + 7 for performing a modulo, according to the Euler's theorem  (where a and p are relatively prime), according to the present problem is that 
it is 2 n-  % 1e9 + 7 can be deformed to become so, then the index is certainly less than 1e9 + 7, then can be calculated using the power of the fast.
code show as below:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;

inline long long fast_pow(long long a, int n){
    long long res = 1L;
    while(n){
        if(n&1)  res = res * a % mod;
        n >>= 1;
        a = a * a % mod;
    }
    return res;
}

int main(){
    string s;
    while(cin >> s){
        long long ans = 0;
        for(int i = 0; i < s.size(); ++i)
            ans = (ans * 10 + s[i] - '0') % (mod - 1);
        ans = (ans - 2 + mod) % (mod - 1);
        cout << (fast_pow(2, ans) + fast_pow(4, ans)) % mod << endl;
    }
    return 0;
}
 
    

Guess you like

Origin www.cnblogs.com/dwtfukgv/p/11222716.html