ACM暑期集训17

刷题打卡:

B

Two boys Gena and Petya wrote on two strips of paper the same string s that consisted of lowercase Latin letters. Then each boy took one strip, cut it into small pieces so that each piece contained exactly one letter and put all pieces into his pocket. After that Gena and Petya repeated the following procedure until their pockets became empty: they took one piece from their pockets and rejoiced if the letters on these pieces were equal.

Determine the expected number of times the boys rejoiced during this process.

Input

The input contains the only string s which was initially written on Gena's and Petya's strips. The string has length between 1 and 200000, inclusively, and consists of lowercase Latin letters.

Output

Output the only real number — the expected number of times Gena and Petya rejoiced during their business. The absolute or relative error of the answer should not exceed 10 - 9.

Examples

Input

abc

Output

1.000000000000000

Input

zzz

Output

3.000000000000000

分析:

统计每个字母出现的次数,算出期望即可

#include <stdio.h>
#include <string.h>
char s[200010];
int cnt[30];
int main()
{
	scanf("%s",&s);
	int n=strlen(s);
	for(int i=0;i<n;i++)
	{
		cnt[s[i]-'a']++;
	}
	double ans=0.0;
	for(int i=0;i<n;i++)
	{
		ans=ans+(double)cnt[s[i]-'a'];
	}
	printf("%.9lf\n",ans/(double)n);
	return 0;
 } 

D

Everyone knows that the battle of Endor is just a myth fabled by George Lucas for promotion of his movie. Actually, no battle of Endor has happened and the First Galactic Empire prospers to this day.

There are creatures of n races living in the First Galactic Empire. In order to demonstrate their freedom, equality and brotherhood the Emperor commanded to introduce the holidays. During each of these holidays creatures of one non-empty subset of races should give gifts to creatures of another non-empty subset of races, not intersecting the first one.

The Emperor's stuff is not very strong in maths so you should calculate how many such holidays can be introduced. Two holidays are considered different if they differ in the subset of races which give gifts or in the subset of races which receive gifts.

Input

The input contains the only integer n (1 ≤ n ≤ 200000) — the number of races living in the First Galactic Empire.

Output

Find the number of holidays the Emperor commanded to introduce. This number can be very large, so output the reminder of division of this number by 109 + 9.

Examples

Input

2

Output

2

Input

3

Output

12

Input

5

Output

180

Input

200000

Output

82096552

大意:每一个集合都可以是一个种族,每个假日选定一些种族作为一个集合,这个集合应该给任何与他没有相交的种族礼物,有n个种族,问你最后要给多少假日?

在每一个假日中,一个非空的种族的成员都应该给另一个非空的种族的成员提供礼物。

两个假日中,如果集合分别给不同的种族礼物,那么这两个假日被认为是不同的 。 

 

题解:首先选出 i 个种族作为一个集合(送礼物的),然后给剩下 n-i 个种族送礼物。即:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define MAX_N 200005
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;

LL fac[MAX_N];//阶乘表 

void init(){
    fac[1]=1;
    for(int i=2;i<MAX_N;i++)
        fac[i]=fac[i-1]*i%MOD;
}
//扩展欧几里得 
LL exGcd(LL a,LL b,LL &x,LL &y){
    if(b==0){
        x=1,y=0;
        return a;
    }
    LL q=exGcd(b,a%b,y,x);
    y-=a/b*x;
    return q;
}
//逆元 
LL mod_inverse(LL a){
    LL x,y;
    exGcd(a,MOD,x,y);
    return (MOD+x)%MOD;
}

LL mod_pow(LL a,int n){
    LL res=1;
    while(n){
        if(n&1) res=res*a%MOD;
        a=a*a%MOD;
        n>>=1;
    }
    return res;
}

int main()
{
    init();
    int n;
    scanf("%d",&n);
    LL ans=0;
    for(int i=1;i<n;i++){
        ans+=fac[n]*mod_inverse(fac[n-i])%MOD*mod_inverse(fac[i])%MOD*(mod_pow(2,n-i)-1)%MOD;
        ans%=MOD;
    }
    printf("%lld\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值