hdu 3398
String
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2161 Accepted Submission(s): 628
lxhgww received a task : to generate strings contain '0's and '1's
only, in which '0' appears exactly m times, '1' appears exactly n times.
Also, any prefix string of it must satisfy the situation that the
number of 1's can not be smaller than the number of 0's . But he can't
calculate the number of satisfied strings. Can you help him?
Each case contains two numbers n and m( 1 <= m <= n <= 1000000 ).
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
const long long mod = ; bool s[];
int prime[],len;
void Init(){
int i,j;
memset(s,false,sizeof(s));
len=;
for(i=;i<=;i++)
{
if(s[i]==true) continue;
prime[++len]=i;
if(i>) return;
for(j=i*;j<=;j=j+i)
s[j]=true;
}
}
int get_num(int n,int m){
int ans=;
while(n){
n=n/m;
ans=ans+n;
}
return ans;
}
LL pow_mod(LL a,LL b)
{
LL ans=;
while(b)
{
if(b&){
ans=(ans*a)%mod;
}
b=b>>;
a=(a*a)%mod;
}
return ans;
} void solve(int n,int m){
int ans;
LL sum1=,sum2=;
for(int i=;i<=len;i++)
{
if(prime[i]>n+m)break;
ans=get_num(n+m,prime[i])-get_num(n,prime[i])-get_num(m,prime[i]);
sum1=(sum1*pow_mod(prime[i],ans))%mod; ans=get_num(n+m,prime[i])-get_num(n+,prime[i])-get_num(m-,prime[i]);
sum2=(sum2*pow_mod(prime[i],ans))%mod;
}
// printf("%lld %lld\n",sum1,sum2);
if(sum1<sum2) sum1=sum1-sum2+mod;
else sum1=sum1-sum2;
printf("%lld\n",sum1);
}
int main()
{
Init();
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
solve(n,m);
}
return ;
}
hdu 3398的更多相关文章
- HDU 3398 String
题目大意:一个长为n的01字符串,使前缀任意0的数量不大于1的数量,求方案数…… 题解:高一模拟赛时做过,是卡特兰数的几何意义,将字符串变为矩阵寻路,不可越过对角线,那么就是卡特兰数了,C(n+m, ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...
- bzoj1834 [ZJOI2010]network 网络扩容
第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...
- mysql设置时区方法
set global time_zone = '+2:00'; ##修改mysql全局时区 set time_zone = '+2:00'; ##修改当前会话时区 flush privileges; ...
- 夺命雷公狗---DEDECMS----25dedecms导航条首页的跳转
我们的导航条首页一直没写跳转,现在我们看看dede自带模版的首页是怎么写的: 他也是封装了一个{dede:global.cfg_cmsurl/}/内置标签来实现的,那么我们来改下我们的head.htm ...
- [php] How to debug PHP in the terminal
Here I use Netbeans, xdebug to debug the PHP in the terminal of Ubuntu. 1. you have to install the x ...
- [fedora21]给fedora21安装fcitx输入法
如果已经安装了ibus,需要卸载ibus: sudo yum remove ibus; 安装fcitx: sudo yum install fcitx fcitx-pinyin fcitx-con ...
- yii2的GridView和ActiveDataProvider具体使用
1.控制器中(以User模块的列表为例): 第一步: use backend\models\User;use yii\data\ActiveDataProvider; 第二步: public func ...
- shell expr 文件扩展名判断 整数判断
expr "text.sh" : ".*\.sh" &>/dev/null && echo "yes" ||e ...
- linux 文件删除原理
文件删除: i_link 文件的硬连接数 i_count 引用计数(有一个程序使用i_count加1) 文件删除的条件: i_link=0 and i_count=0 被进程占用的文件可以删除
- 161021、spring异步调用,完美解决!
前言 项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步 ...