现在的位置: 首页 > 综合 > 正文

Codeforces Round #295 (Div. 2) C. DNA Alignment

2018年05月03日 ⁄ 综合 ⁄ 共 2592字 ⁄ 字号 评论关闭
C. 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

题目中 ρ(s, t)定义如下变换

技术分享

t中的每个DNA都会和s中的DNA匹配N次,那么只要找出S中出现次数最多的字母就可以了,但是,如果出现相同次数的,可以选择任意一个,因此答案就是kn
 

k表示出现次数同样最多的字母的个数。


#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#define N 100009
typedef __int64 LL;
#define MOD 1000000007
using namespace std;

char str[N];
int sum[4];

int cmp(int a,int b)
{
    return a>b;
}

LL pow_mod(LL a,LL n)
{
    if(n==0) return 1LL;
    LL ans=pow_mod(a,n>>1);
    ans=ans*ans%MOD;
    if(n%2==1) ans=ans*a%MOD;
    return ans;
}

int fun(char s)
{
    if(s=='A') return 0;
    if(s=='C') return 1;
    if(s=='G') return 2;
    return 3;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s",str);
        memset(sum,0,sizeof sum);

        for(int i=0;i<n;i++)
            sum[fun(str[i])]++;

        sort(sum,sum+4,cmp);

        int t=1;
        while(t<4 && sum[t]==sum[t-1]) t++;

        LL ans;
        ans=pow_mod(t,n);

        printf("%I64d\n",ans);

    }

    return 0;
}




抱歉!评论已关闭.