这场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的更多相关文章

  1. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  2. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  3. codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  4. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle

    水题,注意数据范围即可 #include <iostream> #include <algorithm> #include <utility> using name ...

  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

随机推荐

  1. Python fopen,open,和popen的区别

    1.  fopen     打开普通文件 带缓冲区撒点粉撒点粉阿桑地方 缓冲文件系统是借助文件结构体指针来对文件进行管理,通过文件指针来对文件进行访问,既可以读写字符.字符串.格式化数据,也可以读写二 ...

  2. Datasets for Data Mining and Data Science

    https://github.com/mattbane/RecommenderSystem http://grouplens.org/datasets/movielens/ KDDCUP-2012官网 ...

  3. Intersection of Two Arrays | & ||

    Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...

  4. js指定标签的id只能添加不能删除

    <body> <form id="form1" runat="server"> <div> <input id=&qu ...

  5. SSHPASS支持从命令行输入密码

    参考:http://www.2cto.com/os/201307/227911.html 手动下载地址:http://sourceforge.net/projects/sshpass/ 安装示例: w ...

  6. Java for LeetCode 069 Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...

  7. git merge和个git rebase的区别

    http://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase/16 ...

  8. java类初始化优先级

    父类静态变量.父类静态代码块.子类静态变量.子类静态代码块.父类非静态变量.父类非静态代码块.父类构造函数.子类非静态变量.子类非静态代码块.子类构造函数

  9. 原始套接字(SOCK_RAW)

    本文转载:http://www.cnblogs.com/duzouzhe/archive/2009/06/19/1506699.html,在此感谢 原始套接字(SOCK_RAW). 应用原始套接字,我 ...

  10. 6.原型模式(Prototype Pattern)

    using System; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { // 孙 ...