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也就是"广播接收者"的意思,顾名思义,它就是用来 ...
随机推荐
- 查询set、dict、dict.keys()的速度对比
查找效率:set>dict>list 单次查询中: list set dict O(n) set做了去重,本质应该一颗红黑树 (猜测,STL就是红黑树),复杂度 O(logn): dict ...
- transition和animation区别
Transform:对元素进行变形: Transition:对元素某个属性或多个属性的变化,进行控制(时间等),类似flash的补间动画.但只有两个关键贞.开始,结束. Animation:对元素某个 ...
- FAFU 1136 最长递增子序列
http://acm.fafu.edu.cn/problem.php?id=1136 根据dp建边,建边的时候记得判断如果原本数的大小就ok了 好久没在自家OJ上刷了 #include <ios ...
- Cookie学习笔记
1.简介 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网站时,由web服务器放置于你硬盘上的 ...
- Linux系统下C语言程序的构建过程
本文转载自:http://www.ruanyifeng.com/blog/2014/11/compiler.html 源码要运行,必须先转成二进制的机器码.这是编译器的任务. 比如,下面这段源码(假定 ...
- git仓库按时间、成员等维度分析统计
git 按时间打印所有成员代码提交: git log --since ==2018-01-01 --until=2018-12-31 --format='%aN' | sort -u | while ...
- web前端----html表单操作
form表单 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含textarea.selec ...
- CentOS随笔 - 4.CentOS7安装MySql 5.5.60(下载 tar 方式安装)
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 由于公司也有php+mysql的项目, 所以今天也把Mysql装了一遍. 为了与以前的程序和数据库兼容, 这次 ...
- mysql5.6升级及mysql无密码登录
mysql5.6升级 mysql5.6的升级可以分为以下几个步骤: 安全关闭正在运行的MySQL实例 把/usr/local/mysql 的连接由MySQL5.6更改为MySQL5.7 启动MySQL ...
- MySQL新建用户保存的时报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
又是这种错, 以前没遇过, 没办法, 直接google. 下面看解决办法: 登录mysql, 当然了如果您登录不上(密码错误情况), 直接扔这个属性进去my.cnf配置文件skip-grant-tab ...