hdu 5651 xiaoxin juju needs help 逆元 两种求解方式
xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1159 Accepted Submission(s): 335
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
For each test case, there is a single line containing a string S(1≤length(S)≤1,000).
aa
aabb
a
2
1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
char a[];
ll flag[];
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll combine1(ll n,ll m) //计算组合数C(n,m)
{
ll sum=; //线性计算
for(ll i=,j=n;i<=m;i++,j--)
{
sum*=j;
sum%=mod;
ll x,y;
extend_Euclid(i,mod,x,y);
sum*=(x%mod+mod)%mod;
sum%=mod;
}
return sum;
}
int main()
{
ll x,y,z,i,t;
scanf("%I64d",&z);
while(z--)
{
memset(flag,,sizeof(flag));
scanf("%s",a);
x=strlen(a);
for(i=;i<x;i++)
flag[a[i]-'a']++;
ll sum=;
for(i=;i<;i++)
{
if(flag[i]%)
sum++;
}
if(x%==&&sum)
printf("0\n");
else if(x%==&&sum>)
printf("0\n");
else
{
ll f=x/;
ll ans=;
for(i=;i<;i++)
{
if(flag[i]/)
{
ans*=combine1(f,flag[i]/);
f-=flag[i]/;
ans%=;
}
}
printf("%I64d\n",ans);
}
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
char a[];
ll flag[];
ll poww(ll a,ll n)//快速幂
{
ll r=,p=a;
while(n)
{
if(n&) r=(r*p)%mod;
n>>=;
p=(p*p)%mod;
}
return r;
}
ll combine1(ll n,ll m) //计算组合数C(n,m)
{
ll sum=; //线性计算
for(ll i=,j=n;i<=m;i++,j--)
{
sum*=j;
sum%=mod;
sum*=poww(i,);
sum%=mod;
}
return sum;
}
int main()
{
ll x,y,z,i,t;
scanf("%I64d",&z);
while(z--)
{
memset(flag,,sizeof(flag));
scanf("%s",a);
x=strlen(a);
for(i=;i<x;i++)
flag[a[i]-'a']++;
ll sum=;
for(i=;i<;i++)
{
if(flag[i]%)
sum++;
}
if(x%==&&sum)
printf("0\n");
else if(x%==&&sum>)
printf("0\n");
else
{
ll f=x/;
ll ans=;
for(i=;i<;i++)
{
if(flag[i]/)
{
ans*=combine1(f,flag[i]/);
f-=flag[i]/;
ans%=;
}
}
printf("%I64d\n",ans);
}
}
return ;
}
hdu 5651 xiaoxin juju needs help 逆元 两种求解方式的更多相关文章
- HDU 5651 xiaoxin juju needs help 逆元
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
- HDU 5651 xiaoxin juju needs help 数学
xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...
- HDU 5651 xiaoxin juju needs help (组合数)
xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...
- HDU 5651 xiaoxin juju needs help
组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- HDU 5651 xiaoxin juju needs help 水题一发
分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...
- hdu5651 xiaoxin juju needs help(逆元)
xiaoxin juju needs help Accepts: 150 Submissions: 966 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
随机推荐
- [py][mx]xadmin详细设置-将app注册到xadmin
首先createsuperuser,创建用户, 然后登陆xadmin. 理解下models的各个字段 复数形式及返回 注册app users/adminx.py 显示字段 users/adminx.p ...
- 包管理 ----- Linux操作系统rpm包安装方式步骤
Linux操作系统rpm包安装方式步骤 2016年08月04日 07:00:26 阅读数:17140 转自 : http://os.51cto.com/art/201003/186467.htm 特别 ...
- data.frame和matrix的一些操作
编写脚本的时候经常会涉及到对data.frame或matrix类型数据的操作,比如取指定列.取指定行.排除指定列或行.根据条件取满足条件的列或行等.在R中,这些操作都是可以通过简单的一条语句就能够实现 ...
- Selenium Webdriver——操作隐藏的元素(二)display属性
有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...
- The Air Jordan 11 Gym Red will be available December 9
A few years ago Carmelo Anthony set the internet on fire when he was spotted rocking a never before ...
- TraceSource记录程序日志
1.配置文件 <system.diagnostics> <sources> <source name="TraceError" switchValue ...
- SQL Server 将查询结果导出插入(insert)语句的简单方式
转自 http://blog.csdn.net/danny_style/article/details/45166391 1.首先将查询结果添加到一个原数据库中不存在的表,表名随意命名. 例:SELE ...
- ngxin开启rewrite伪静态
1.编辑nginx配置文件 vi /usr/local/nginx/conf/nginx.conf #include enable-php.conf; include enable-php-pathi ...
- python 内置函数bytearray
1.参考文档 class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cl ...
- Java设计模式应用——工厂模式
工厂模式有三种:简单工厂.工厂方法.抽象工厂 一. 抽象工厂 1. 一个可以生产多种产品的工厂: 2. 不改变工厂无法生产新的产品. package com.coshaho.learn.factory ...