lightoj 1102 - Problem Makes Problem
1102 - Problem Makes Problem
As I am fond of making easier problems, I discovered a problem. Actually, the problem is 'how can you make n by adding k non-negative integers?' I think a small example will make things clear. Suppose n=4and k=3. There are 15 solutions. They are
1. 0 0 4
2. 0 1 3
3. 0 2 2
4. 0 3 1
5. 0 4 0
6. 1 0 3
7. 1 1 2
8. 1 2 1
9. 1 3 0
10. 2 0 2
11. 2 1 1
12. 2 2 0
13. 3 0 1
14. 3 1 0
15. 4 0 0
As I have already told you that I use to make problems easier, so, you don't have to find the actual result. You should report the result modulo 1000,000,007.
Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.
Each case contains two integer n (0 ≤ n ≤ 106) and k (1 ≤ k ≤ 106).
Output
For each case, print the case number and the result modulo 1000000007.
Sample Input |
Output for Sample Input |
|
4 4 3 3 5 1000 3 1000 5 |
Case 1: 15 Case 2: 35 Case 3: 501501 Case 4: 84793457 |
http://www2.chinaedu.com/101resource004/wenjianku/200501/101ktb/lanmu/XF1S0213/XF1S0213.htm
隔板法
思路:组合数学,费马小定理求逆元,快速幂。
用隔板法来求,这个问题可以转化为x1+x2+...xk=n的多元方不同程解的个数,并且xk〉=0;
就是组合数C(n+k-1,k-1) ,那么由费马小定理ap-1==1mod(p);设a-1为a的逆元则(a*a-1*ap-2)=a-1mod(p);
即ap-2=a-1mod(p);C(a,b) =(f(a))/(f(b)*f(a-b));
C(a,b)%p=((f(a))/(f(b)*f(a-b)))%P;其中f(n)表示阶乘。
(a/b)%p=k%p;两边同乘b ----a%p=(k*b)%p;然后两边同乘b-1%p;----a*b-1%p=k%p;
而根据费马小定理ap-2=a-1mod(p);用快速幂求下bp-2%p就可以了。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<queue>
7 using namespace std;
8 const long long N=1e9+7;
9 typedef long long LL;
10 LL quickmi(long long a,long long b);
11 LL DP[2*1000005];
12 void Init()
13 {
14 int i,j;
15 DP[0]=1;
16 for(i=1;i<=2000005;i++)
17 {
18 DP[i]=(DP[i-1]*i)%N;
19 }
20 }
21 int main(void)
22 {Init();
23 int i,j,k,p,q;
24 scanf("%d",&k);
25 for(i=1;i<=k;i++)
26 {
27 scanf("%d %d",&p,&q);
28 LL ans=quickmi(DP[q-1]*DP[p]%N,N-2);
29 printf("Case %d: ",i);
30 printf("%lld\n",DP[p+q-1]*ans%N);
31 }
32 return 0;
33 }
34
35 LL quickmi(long long a,long long b)
36 {
37 LL sum=1;
38 while(b)
39 {
40 if(b&1)
41 sum=(sum*a)%(N);
42 a=(a*a)%N;
43 b/=2;
44 }
45 return sum;
46 }
lightoj 1102 - Problem Makes Problem的更多相关文章
- (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...
- Lightoj 1004 - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/121396#problem/F http://lightoj.com/volume_showproblem.ph ...
- light oj 1102 - Problem Makes Problem组合数学(隔板法)
1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...
- LightOJ - 1102 - Problem Makes Problem(组合数)
链接: https://vjudge.net/problem/LightOJ-1102 题意: As I am fond of making easier problems, I discovered ...
- 不可解问题之停机问题(Undecidable Problem Halting Problem)
计算机技术已运用到人类生活的方方面面,帮助人类解决各种问题.可你是否有想过,计算机是否能为人类解决所有问题呢? 假如你是一个程序猿,你已编写过很多程序.有些程序一下子就能出结果,有些程序则好久都没有显 ...
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- LightOJ 1070 - Algebraic Problem 矩阵高速幂
题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- Training - Problem and Change Management
Problem Management Problem management seeks to identify the underlying causes of incidents in an IT ...
随机推荐
- SPI详解2
串行外设接口 (SPI) 总线是一种运行于全双工模式下的同步串行数据链路.用于在单个主节点和一个或多个从节点之间交换数据. SPI 总线实施简单,仅使用四条数据信号线和控制信号线(请参见图 1). 图 ...
- A Child's History of England.2
They made boats of basket-work, covered with the skins of animals, but seldom, if ever, ventured far ...
- Hadoop 相关知识点(一)
作业提交流程(MR执行过程) Mapreduce2.x Client:用来提交作业 ResourceManager:协调集群上的计算资源的分配 NodeManager:负责启动和监控集群上的计算容器( ...
- midi的一些概念,包括一些音乐的概念
参考:http://www.yueqixuexi.com/jita/20180918205363.html https://blog.csdn.net/meicheng777/article/deta ...
- java中的collection小结
Collection 来源于Java.util包,是非常实用常用的数据结构!!!!!字面意思就是容器.具体的继承实现关系如下图,先整体有个印象,再依次介绍各个部分的方法,注意事项,以及应用场景. ...
- Linux基础命令---elinks文本浏览器
elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- ReactiveCocoa操作方法-线程\时间
ReactiveCocoa操作方法-线程 deliverOn: 内容传递切换到制定线程中,副作用在原来线程中,把在创建信号时block中的代码称之为副作用. subscribeOn: 内容传递和副作用 ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- 【C/C++】从矩阵左上角走到右下角
tx的笔试,但是只过了10%,就离谱 #include <bits/stdc++.h> using namespace std; const int maxn = 1010; long d ...
- thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库
最近项目中遇到引入PHPExcel第三方类库 但是下载的phpExcel类没有命名空间,而且所有接口文件的命名都是以.php结尾 而不是tp中的.class.php 解决办法很简单:在引入没有采用命 ...