这场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. linux 使用 ionice 限制 Xen 虚拟机磁盘 IO

    作为 VPS 服务商我们需要保证每个 VPS 公平的使用 host(服务器)的资源,避免某个 VPS 因为程序死循环.挂起.滥用等因素 “拖累” 其他 VPS,如果出现这个情况如何临时限制这个 VPS ...

  2. Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  3. 72 [面试题]如果不使用if-else和比较运算符,你知道如何求解2个数字中的较大一个吗?

    [本文链接] http://www.cnblogs.com/hellogiser/p/max-of-numbers-without-comparations.html [题目] 不使用if-else和 ...

  4. Ribbon_窗体_实现Ribbon风格的窗体

    Ribbon_窗体_实现Ribbon风格的窗体 随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢? ...

  5. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  6. VelocityTracker简介

    android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率.用addMovement(MotionEvent)函数将Mot ...

  7. weblogic 安装和部署项目(原创)

    1.下载weblogic(含破解文件,土豪请支持正版,谢谢!) 2.安装如下图: 3.新建domain 4.打开weblogic Console 5.开始部署项目 6.部署成功

  8. Xamarin.Android开发实践(十四)

    Xamarin.Android之ListView和Adapter 一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xamarin去实现它,以及如何使用适配器和自定义适配器(本文 ...

  9. Xamarin.Android开发实践(七)

    Xamarin.Android广播接收器与绑定服务 一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架 ...

  10. AndroidManifest.xml相关知识

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest  ...