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 ...
随机推荐
- hdu4255筛素数+广搜
Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...
- sharepoint bcs (bussiness connectivity services)
sharepoint bcs 在2010 版本中是提供2010 与外部数据连接的. BCS全名Business Connectivity Services,可以把它看成SharePoint 2007 ...
- 利用 Chromium Embedded Framework (CEF) 定制提取 Flash 视频的浏览器
功能介绍: 利用 CEF 分析网页源码, 提取 flash 视频的代码. 提取的视频代码 LoadString 和 JS 两种方式重新插入到浏览器. (CEF_3.2171.1979_win32 - ...
- ext树表+ZeroClipboard复制链接功能
效果图:
- ubuntu安装vmware tools
1.选择虚拟机菜单栏--安装VMware tools 选择你装虚拟机下的目录,点击 Linux.iso 再重新点击安装VMware Tools 这样会弹出一个包VMwareTools-9.6.0-1 ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续
4.2.1.2 Fresco.initializeDrawee()的过程 续 继续上篇博客的分析Fresco.initializeDrawee() sDraweeControllerBuilderSu ...
- 提高IO性能
noatime - 不更新文件系统上 inode 访问记录,可以提升性能 [root@ok etc]# cat /etc/fstab |grep noatime /dev/mapper/vg_ok-l ...
- Linux下如何设置和查看环境变量
Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1 永久的:需要修改配置文件,变量永久生效. 2 临时的:使用export命令声明即可,变量在关闭shell时失效. 按作用范 ...
- win + linux + android 多任务分屏
Win10 的多任务分屏操作方法_百度经验http://jingyan.baidu.com/article/48206aeaf6ef35216ad6b336.html win+方向键 如果是想四分屏幕 ...
- 更改Windows控制台默认缓冲区行数和宽度
Windows控制台窗口默认的显示行数很少, 以至于有时候要显示很多东西的时候, 总是导致前面的内容丢失. 很不爽. 于是GG了下, 在StackOverflow上找到了答案~~~ 设置方式: 1. ...