Codeforces 520C. DNA Alignment 找规律 (简单快速幂5)

本文探讨了一种计算DNA序列相似性的新方法,并提出了如何计算具有最大Vasya距离的字符串数量。通过分析字符串中字符出现频率,利用数学公式进行求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F - DNA Alignment
Crawling in process... Crawling failed Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

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 functionh(s, t) is defined as the number of positions in which the respective symbols ofs 

and t arethe same. Functionh(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 modulo109 + 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 Input

Input
     
1
C
Output
1
Input
     
2
AG
Output
4
Input
     
3
TTT
Output
1

Sample Output

Hint

Please note that if for two distinct strings t1 andt2 valuesρ(s, t1) иρ(s, t2) are maximum among all possiblet, 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 stringst 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

分析:这道题读了好久才弄明白是什么意思,实际上就是求出现最多的次数的字符,若出现次数最多的字母不唯一,那么相当于B串每一位我们都有不唯一的选择,

设我们有x种选择,那么最后构造出的B串种数就是ans^n,因此我们就可以得到最终答案:ans^n 取摸。


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
#define ll long long
const ll M=1000000007;
int n;
string input;
map<char,ll> a;
const string s="ATCG";
ll maxn=-1,cnt,ans=1;
int main()
{
    cin>>n>>input;
    for(int i=0;i<n;i++)
        a[input[i]]++;
    for(int i=0;i<4;i++)//找出出现次数最多的字符
        if(a[s[i]]==maxn)
            cnt++;
        else if(a[s[i]]>maxn)
        {
            maxn=a[s[i]];
            cnt=1;
        }
    while(n)
    {
    if(n&1)ans*=cnt;
    ans%=M;
    n>>=1;
    cnt=cnt*cnt%M;}

    cout<<ans<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值