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 ...
随机推荐
- 34、在排序数组中查找元素的第一个和最后一个位置 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(34)在排序数组中查找元素的第一个和最后一个位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: / ...
- for no other reason than because
在狄更斯的<A Child History of England>中有段话: After some disputing among the priests, who said that a ...
- ubantu上编辑windows程序
命令简记 cd $GOROOT/src cp -r $GOROOT /root/go1.4 CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash 操作 ...
- Output of C++ Program | Set 3
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...
- Mysql的表级锁
我们首先需要知道的一个大前提是:mysql的锁是由具体的存储引擎实现的.所以像Mysql的默认引擎MyISAM和第三方插件引擎 InnoDB的锁实现机制是有区别的.可根据不同的场景选用不同的锁定机制. ...
- 【编程思想】【设计模式】【结构模式Structural】MVC
Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python ...
- springboot-MVC 过滤器使用
一.前言 一下代码以SSO用户登录列子代码.完整代码https://gitee.com/xuxueli0323/xxl-sso 二.使用 2.1 创建过滤器 创建一个过滤器,实现Filter 接口 p ...
- jstl中的choose标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ ta ...
- SQLServer和java数据类型的对应关系
转载自:https://www.cnblogs.com/cunkouzh/p/5504052.html SQL Server 类型 JDBC 类型 (java.sql.Types) Java 语言类型 ...
- 我的第一篇博客blog,笑哭
我的第一篇博客blog Markdown学习 一级标题:#加一个空格 加 文字, 二级标题:加2个##以此类推 字体 粗体:hello world!字体前有二个星号,字体后有二个星号 斜体:hello ...