HDU 3037 Saving Beans (数论,Lucas定理)
题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法。
析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m)。
然后就求这个值,直接求肯定不好求,所以我们可以运用Lucas定理,来分解这个组合数,也就是Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p)。
然后再根据费马小定理就能做了。
代码如下:
第一种:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
LL exgcd(LL a,LL b,LL &x,LL &y){LL d = a;if(b!=0){d=exgcd(b,a%b,y,x);y-=(a/b)*x;}else{x=1;y=0;}return d;}
LL mod_inverse(LL a,LL m){LL x,y;exgcd(a,m,x,y);return (m+x%m)%m; }
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL fact[100005]; LL mod_fact(LL n, LL p, LL &e){
e = 0;
if(!n) return 1;
LL res = mod_fact(n / p, p, e);
e += n / p;
if(n / p % 2 != 0) return res * (p - fact[n%p]) % p;
return res * fact[n%p] % p;
} LL mod_comb(LL n, LL k, LL p){
if(n < 0 || k < 0 || n < k) return 0;
LL e1, e2, e3;
LL a1 = mod_fact(n, p, e1);
LL a2 = mod_fact(k, p, e2);
LL a3 = mod_fact(n-k, p, e3);
if(e1 > e2+e3) return 0;
return a1 * mod_inverse(a2*a3%p, p) % p;
} int main(){
fact[0] = 1;
int T; cin >> T;
while(T--){
LL p, m, n;
scanf("%I64d %I64d %I64d", &n, &m, &p);
for(int i = 1; i < p; ++i) fact[i] = fact[i-1] * (LL)i % p;
printf("%I64d\n", mod_comb(n+m, m, p));
}
return 0;
}
第二种:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL fact[100005];
LL p;
LL quick_pow(LL a, LL b){
LL ans = 1LL;
a %= p;
while(b){
if(b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
} LL C(LL n, LL m){
if(n < m) return 0;
return fact[n] * quick_pow(fact[m]*fact[n-m], p-2) % p;
} LL lucas(LL n, LL m){
if(!m) return 1LL;
return C(n%p, m%p) * lucas(n/p, m/p) % p;
} int main(){
fact[0] = 1;
int T; cin >> T;
while(T--){
LL m, n;
scanf("%I64d %I64d %I64d", &n, &m, &p);
for(int i = 1; i < p; ++i) fact[i] = fact[i-1] * (LL)i % p;
printf("%I64d\n", lucas(n+m, m));
}
return 0;
}
第三种:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL p;
LL quick_pow(LL a, LL b){
LL ans = 1LL;
a %= p;
while(b){
if(b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
} LL C(LL n, LL m){
if(n < m) return 0;
LL a = 1, b = 1;
while(m){
a = a * n % p;
b = b * m % p;
--m; --n;
}
return a * quick_pow(b, p-2) % p;
} LL lucas(LL n, LL m){
if(!m) return 1LL;
return C(n%p, m%p) * lucas(n/p, m/p) % p;
} int main(){
int T; cin >> T;
while(T--){
LL m, n;
scanf("%I64d %I64d %I64d", &n, &m, &p);
printf("%I64d\n", lucas(n+m, m));
}
return 0;
}
HDU 3037 Saving Beans (数论,Lucas定理)的更多相关文章
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- HDU 3037 Saving Beans (Lucas法则)
主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- hdu 3037 Saving Beans Lucas定理
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3037 Saving Beans(Lucas定理的直接应用)
解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...
- Hdu 3037 Saving Beans(Lucus定理+乘法逆元)
Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...
- hdu 3037——Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu3037 Saving Beans(Lucas定理)
hdu3037 Saving Beans 题意:n个不同的盒子,每个盒子里放一些球(可不放),总球数<=m,求方案数. $1<=n,m<=1e9,1<p<1e5,p∈pr ...
- hdu 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- 【HDOJ5640】King's Cake(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- 51nod1184 第N个质数
如题.$n \leq 1e9$. 方法零:二分,然后洲阁筛.要魔改一下的洲阁筛.跑得慢.卡卡能过.没意思. //#include<iostream> #include<cstring ...
- msp430项目编程02
msp430中项目---液晶1602显示 1.液晶1602工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习
- Tomcat错误信息(服务器版本号)泄露(低危)
一.问题描述Tomcat报错页面泄漏Apache Tomcat/7.0.52相关版本号信息,是攻击者攻击的途径之一.因此实际当中建议去掉版本号信息. 二.解决办法 1.进入到tomcat/lib目录下 ...
- how to read openstack code: action extension
之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...
- Openwrt 安装软件到U盘或硬盘
http://blog.licess.org/openwrt-install-software-to-udisk-harddisk/ 运行一个多月的DDNAS被结婚来玩的小孩给关了,于是趁机更新了一下 ...
- 微信小程序 常见问题 小结
1.微信小程序 尺寸单位 rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...
- 【Mongodb教程 第一课 补加课1 】windows7 下安装mongodb 开启关闭服务
mongodb在2.2版本开始就不支持windows xp了(我想现在用xp的应该也是带着情怀的一部分人吧,我只是一个工匠而已),windows下server8 R2,64位,32位,只是32位只支持 ...
- Swift开发--Storyboard的使用教程
假设App中包含非常多不同的页面,使用Storyboard能够帮你降低实现页面间跳转的胶合代码. 过去的开发人员相应每一个视图控制器分别创建界面设计文件(即"nib"或" ...
- linux【第六篇】用户和用户管理及定时任务复习
定时任务复习 1.什么是定时任务? 2.如何编辑查看定时任务(配置文件位置?),语法的特殊字符意义是什么?- * , / 3.书写定时任务有哪些要领? 4.生产如何调试定时任务 5.生产场景配置定时任 ...