Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings
这场CF,脑子乱死啊。。。C题,搞了很长时间,结束了,才想到怎么做。B题,没看,D题,今天看了一下,很不错的组合题。
如果n和m都挺多的时候
以下情况都是变为1,根据偶数个0,最后将会为1,奇数个0,最后变为0,以1开头,全都是0.
0 1..
0 0 0 1....
0 0 0 0 0 1....
总的情况是C(n+m,m),算1也可以算出来。注意m = 1的时候特判,0的时候,我也全写的特判。
10^5的组合数,用费马小定理求逆元。看了下题解,题解直接来了个逆元。。
inv(a) = a^(mod-2),完全没看懂,查了查资料,明白了。。
a*inv(a) 模 mod = 1
因为mod是素数,根据费马小定理,a的p-1次方 对 p取余等于1, a^(mod-2)肯定是逆元。
算组合数,好高端啊。。。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define MOD 1000000007
#define LL __int64
LL fact[];
LL fastmod(LL a,LL k)
{
LL b = ;
while(k)
{
if(k&)
b = a*b%MOD;
a = (a%MOD)*(a%MOD)%MOD;
k = k/;
}
return b;
}
LL comb(int n,int m)//如果取余是素数,根据费马小定理求逆元,快速求组合数
{
LL ans,a;
ans = fact[n];
a = fastmod((fact[n-m]*fact[m])%MOD,MOD-);
return (ans*a)%MOD;
}
int main()
{
int i,n,m,g;
LL ans,res;
scanf("%d%d%d",&n,&m,&g);
if(m == )
{
if(n% == )
{
printf("%d\n",g);
}
else
{
printf("%d\n",!g);
}
return ;
}
if(n == )
{
if(m == &&g == )
printf("1\n");
else if(m == &&g == )
printf("0\n");
else if(m > &&g == )
printf("1\n");
else
printf("0\n");
return ;
}
fact[] = ;
for(i = ;i <= n+m;i ++)
{
fact[i] = (fact[i-]*i)%MOD;
}
ans = comb(n+m,n);
res = ;
for(i = ;i <= n;i += )
{
if(m == ) break;
if(i + == n+m) break;
res = (res + comb(n+m-i-,m-))%MOD;
}
if(m == &&n% == )
res ++;
if(g == )
printf("%I64d\n",res);
else
{
ans = (ans - res + MOD)%MOD;
printf("%I64d\n",ans);
}
return ;
}
Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Beautiful Strings Vas ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle
水题,注意数据范围即可 #include <iostream> #include <algorithm> #include <utility> using name ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
随机推荐
- Android自动登录与记住密码
// 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...
- MySQL之扩展(触发器,存储过程等)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- OpenStack
[官网]http://www.openstack.org/ [视频教程1]http://blog.csdn.net/u010973404/article/details/16841229 [视频教程2 ...
- python 异常类型
1.NameError:尝试访问一个未申明的变量>>> vNameError: name 'v' is not defined 2.ZeroDivisionError:除数为0&g ...
- 转数据库Sharding的基本思想和切分策略
本文着重介绍sharding的基本思想和理论上的切分策略,关于更加细致的实施策略和参考事例请参考我的另一篇博文:数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示 一.基本思想 Sh ...
- Gym 100801E Easy Arithmetic (思维题)
题目:传送门.(需要下载PDF) 题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式. 题解:比如300-456就改成300-4+56,遇 ...
- HDU1003MAX SUM
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- PrincipalView的使用参数
4 G:\PrincipalView\model\m426.off 注意,路径是绝对路径,所以如果程序移位的话,要注意修改: 路径中不能包含空格
- Redis笔记(三)Redis的数据类型
前面说过,Redis的一大特性是支持丰富的数据类型, 这为更多的应用场景提供了可能. Redis有五种数据类型,包括string,list,set,sorted set和hash,注意,Redis的数 ...