这场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)肯定是逆元。

算组合数,好高端啊。。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <string>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. #define MOD 1000000007
  8. #define LL __int64
  9. LL fact[];
  10. LL fastmod(LL a,LL k)
  11. {
  12. LL b = ;
  13. while(k)
  14. {
  15. if(k&)
  16. b = a*b%MOD;
  17. a = (a%MOD)*(a%MOD)%MOD;
  18. k = k/;
  19. }
  20. return b;
  21. }
  22. LL comb(int n,int m)//如果取余是素数,根据费马小定理求逆元,快速求组合数
  23. {
  24. LL ans,a;
  25. ans = fact[n];
  26. a = fastmod((fact[n-m]*fact[m])%MOD,MOD-);
  27. return (ans*a)%MOD;
  28. }
  29. int main()
  30. {
  31. int i,n,m,g;
  32. LL ans,res;
  33. scanf("%d%d%d",&n,&m,&g);
  34. if(m == )
  35. {
  36. if(n% == )
  37. {
  38. printf("%d\n",g);
  39. }
  40. else
  41. {
  42. printf("%d\n",!g);
  43. }
  44. return ;
  45. }
  46. if(n == )
  47. {
  48. if(m == &&g == )
  49. printf("1\n");
  50. else if(m == &&g == )
  51. printf("0\n");
  52. else if(m > &&g == )
  53. printf("1\n");
  54. else
  55. printf("0\n");
  56. return ;
  57. }
  58. fact[] = ;
  59. for(i = ;i <= n+m;i ++)
  60. {
  61. fact[i] = (fact[i-]*i)%MOD;
  62. }
  63. ans = comb(n+m,n);
  64. res = ;
  65. for(i = ;i <= n;i += )
  66. {
  67. if(m == ) break;
  68. if(i + == n+m) break;
  69. res = (res + comb(n+m-i-,m-))%MOD;
  70. }
  71. if(m == &&n% == )
  72. res ++;
  73. if(g == )
  74. printf("%I64d\n",res);
  75. else
  76. {
  77. ans = (ans - res + MOD)%MOD;
  78. printf("%I64d\n",ans);
  79. }
  80. return ;
  81. }

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. hdu4255筛素数+广搜

    Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the fol ...

  2. sharepoint bcs (bussiness connectivity services)

    sharepoint bcs  在2010 版本中是提供2010 与外部数据连接的. BCS全名Business Connectivity Services,可以把它看成SharePoint 2007 ...

  3. 利用 Chromium Embedded Framework (CEF) 定制提取 Flash 视频的浏览器

    功能介绍: 利用 CEF 分析网页源码, 提取 flash 视频的代码. 提取的视频代码 LoadString 和 JS 两种方式重新插入到浏览器. (CEF_3.2171.1979_win32 - ...

  4. ext树表+ZeroClipboard复制链接功能

    效果图:

  5. ubuntu安装vmware tools

    1.选择虚拟机菜单栏--安装VMware tools 选择你装虚拟机下的目录,点击  Linux.iso 再重新点击安装VMware Tools 这样会弹出一个包VMwareTools-9.6.0-1 ...

  6. Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续

    4.2.1.2 Fresco.initializeDrawee()的过程 续 继续上篇博客的分析Fresco.initializeDrawee() sDraweeControllerBuilderSu ...

  7. 提高IO性能

    noatime - 不更新文件系统上 inode 访问记录,可以提升性能 [root@ok etc]# cat /etc/fstab |grep noatime /dev/mapper/vg_ok-l ...

  8. Linux下如何设置和查看环境变量

    Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1 永久的:需要修改配置文件,变量永久生效. 2 临时的:使用export命令声明即可,变量在关闭shell时失效. 按作用范 ...

  9. win + linux + android 多任务分屏

    Win10 的多任务分屏操作方法_百度经验http://jingyan.baidu.com/article/48206aeaf6ef35216ad6b336.html win+方向键 如果是想四分屏幕 ...

  10. 更改Windows控制台默认缓冲区行数和宽度

    Windows控制台窗口默认的显示行数很少, 以至于有时候要显示很多东西的时候, 总是导致前面的内容丢失. 很不爽. 于是GG了下, 在StackOverflow上找到了答案~~~ 设置方式: 1. ...