C. DNA Alignment 数学公式推导 Codeforces Round #295 (Div. 2)

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

题意:给一个由A,T,C,G组成的字符串s,求使ρ(s,t)最大的t的个数。易知,t应该由字符串s中个数最多的字母组成(总数最多的字母只有一个的时候很好理解,如果有多个字母的个数相同,那么就可以将这些个数相同的字母看成一个字母,这样是等价的),求出字母个数最多的种类数x,根据排列组合,答案就是x^n

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

__int64 n;
char str[maxn];
__int64 num[30];

__int64 pow_m(__int64 a,__int64 n)
{
    __int64 ret=1;
    __int64 tmp=a%mod;
    while (n)
    {
        if (n&1) ret=(ret*tmp)%mod;
        tmp=tmp*tmp%mod;
        n>>=1;
    }
    return ret;
}

int main()
{
    while (~scanf("%I64d",&n))
    {
        scanf("%s",str);
        mem(num,0);
        for (__int64 i=0;i<n;i++)
            num[ str[i]-'A' ]++;
        __int64 maxx=0;
        for (__int64 i=0;i<26;i++)
            maxx=max(maxx,num[i]);
        __int64 m=0;
        for (__int64 i=0;i<26;i++)
            if (num[i]==maxx) m++;
        printf("%I64d\n",pow_m(m,n));
    }
    return 0;
}
时间: 2024-08-10 02:09:15

C. DNA Alignment 数学公式推导 Codeforces Round #295 (Div. 2)的相关文章

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

Codeforces Round #295 (Div. 2)

A. Pangram 题意:给出长度为n的字符串,判断26个字母(比如a,A都算作a出现了)是否都在该串中出现了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using namespace std; 7 8 typedef long long LL; 9 10 11 12 int mai

codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)

题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定s串,移动t串时,增加的量为p=a*a'+t*t'+c*c'+g*g'.注意a'+t'+c'+g'是等于串长,那么减少a,t,c,g中最少的对应的那个a',t',c',g',增加到最大的那个上,p值是上升的.而且如果a==t那么a'和t'的数量互换是不影响p值的.因此结论是这种情况下,t串可随意 放

Codeforces Round #295 (Div. 1) C. Pluses everywhere (组合数学+乘法逆元)

这题可以这样想: 对于当前第i位来说,该位若在个位上出现,那么第i位和第i+1位中间肯定有一个"+",剩下的k-1个"+"分布在剩下的n-2个空隙中,所以出现的总次数是C(n-2,k).同理,在十位上出现的总次数是C(n-3,k).于是每个数字的贡献值就可以求出来了,累加即可. 所以大体思路是遍历所有可能出现的位数,从个位开始,分成两部分计算,一部分用前缀和计算出前面所有的在该位上的贡献和,另一部分算出当前位置在该位上的贡献值. 然后对于求组合数,可以先将阶乘预处理

Codeforces Round #295 (Div. 2) A+B+C

A题:找出字符串中是否出现了26个英文字母,不区分大小写 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; char str[200] ; int a[30] , b[30] ; int main() { int n , i ; scanf("%d", &n) ; scanf("%s", str) ; memset

【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #define INF 0x7fffffff; 6 using namespace std; 7 int DP[20100], vis[2010

Codeforces Round #295 (Div. 1) B. Cubes (STL+类拓扑)

最近课业繁重,这题写了两天..昨晚睡觉的时候才突然想到了最后一点的解决方法. 不知道该不该叫做拓扑..感觉还是挺像的..就把标题称之为类拓扑了..这题的方法是用map来标记状态是否存在,然后用类似拓扑的方法不断的找拿走后依然稳定的方块,我用了两个优先队列来维护,分别取最大和最小.然后就是模拟这个过程取方块了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queu

Codeforces Round #295 (Div. 1)

A 简单题 B 有m个方块 每个方块有一个值 并且是堆起来稳定的 一个方块可以拿掉当且仅当剩下的还是稳定的 双方轮流拿 从左到右放组成一个m进制的数 #include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <algorithm> using namespace std; const __int64 mod

Codeforces Round #295 (Div. 2)——A——Pangram

A word or a sentence in some language is called a pangram if all the characters of the alphabet of this language appear in it at least once. Pangrams are often used to demonstrate fonts in printing or test the output devices. You are given a string c