cf.295.C.DNA Alignment(数学推导)

DNA Alignment
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.

Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t):

where  is obtained from string s, by applying left circular shift i times. For example,
ρ("AGC", "CGT") = 
h("AGC", "CGT") + h("AGC", "GTC") + h("AGC", "TCG") + 
h("GCA", "CGT") + h("GCA", "GTC") + h("GCA", "TCG") + 
h("CAG", "CGT") + h("CAG", "GTC") + h("CAG", "TCG") = 
1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6

Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .

Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 105).

The second line of the input contains a single string of length n, consisting of characters "ACGT".

Output

Print a single number — the answer modulo 109 + 7.

Sample test(s)
input
1
C
output
1
input
2
AG
output
4
input
3
TTT
output
1
Note

Please note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.

In the first sample, there is ρ("C", "C") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0.

In the second sample, ρ("AG", "AG") = ρ("AG", "GA") = ρ("AG", "AA") = ρ("AG", "GG") = 4.

In the third sample, ρ("TTT", "TTT") = 27

Tutorial:

What is ρ(s, t) equal to? For every character of s and every character of t there is a unique cyclic shift of t that superposes these characters (indeed, after 0, ..., n - 1 shifts the character in t occupies different positions, and one of them matches the one of the character of s); therefore, there exist n cyclic shifts of s and t that superpose these characters (the situation is symmetrical for every position of the character of s). It follows that the input in ρ from a single character ti is equal to n × (the number of characters in s equal to ti). Therefore, ρ(s, t) is maximal when every character of t occurs the maximal possible number of times in s. Simply count the number of occurences for every type of characters; the answer is Kn, where K is the number of character types that occur in s most frequently. This is an O(n) solution.

others:
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <string>
 5 #include <unordered_map>
 6 #include <cassert>
 7 using namespace std;
 8 int t[4];
 9 long long mod = 1000000007;
10 
11 long long pot(int p, int wyk) {
12     if(wyk == 0) return 1;
13     return (p * pot(p, wyk-1)) % mod;
14 }
15 
16 int main() {
17     ios_base::sync_with_stdio(0);
18     int n;
19     string s;
20     cin >> n >> s;
21     for(auto a: s) {
22         if(a == 'C') t[0]++;
23         if(a == 'T') t[1]++;
24         if(a == 'G') t[2]++;
25         if(a == 'A') t[3]++;
26     }
27     int maxi = 0, licz = 0;
28     for(int i = 0; i < 4; ++i)
29         maxi = max(maxi, t[i]);
30     for(int i = 0; i < 4; ++i)
31         if(t[i] == maxi) licz++;
32     
33     cout << pot(licz, n) << "
";
34     return 0;
35 }
View Code
原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4337390.html