HDU5407.CRB and Candies(数论)
官方题解:
The problem is just to calculate g(N) = LCM(C(N,0),C(N,1),...,C(N,N))
Introducing function f(n) = LCM(1,2,...,n), the fact g(n) = f(n+1)/(n+1) holds.
We calculate f(n) in the following way.
f(1)=1
If n =p^k,then f(n) = f(n−1)× p, else f(n) = f(n−1).
Time complexity:O(N⋅logN)
这题用学的知识:
1。乘法逆元
求(a/b)%c时 化成 (a%c)/(b%c)是错误的,所以需要用到乘法逆元。(a%c)*(b^-1%c)。
b^-1的求法:
费马小定理(Fermat Theory): 假如p是质数,且Gcd(a,p)=1,那么 a^(p-1)(mod p)≡1。
由此可得a*a^(p-2)=1 (mod p) 即a^(p-2)就是a的乘法逆元。通过快速幂可求。
2。LCM(C(N,0),C(N,1),...,C(N,N))=LCM(1,2,...,n)/(n+1)
知乎上看到有人证明,并没有看懂。http://www.zhihu.com/question/34859879
3。求LCM(1,2,...,n)的简便算法
If n =p^k,then f(n) = f(n−1)× p, else f(n) = f(n−1).
代码:
#include <iostream>
#include <cstdio>
#include <cstring> typedef long long ll; const int N = 1000002;
const ll MOD = 1000000007; int v[N + 5];
ll f[N + 5];
int is_p[N + 5];
int p[N + 5];
int cnt_p; int n; void get_p()
{
for (int i = 0; i <= N; ++i) is_p[i] = 1;
cnt_p = 0;
is_p[0] = is_p[1] = 0;
for (int i = 2; i <= N; ++i) {
if (is_p[i]) {
p[cnt_p++] = i;
for (int j = i * 2; j <= N; j += i)
is_p[j] = 0;
}
}
} ll pow(ll a, ll b)
{
ll res = 1;
while (b > 0) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
} ll MIM(ll a)
{
return pow(a, MOD-2);
} void get_f()
{
for (int i = 0; i < cnt_p; ++i) {
ll j = 1;
while (j < N) {
v[j] = p[i];
j *= p[i];
}
}
f[1] = 1;
for (int i = 2; i <= N; ++i) {
if (v[i]) f[i] = f[i - 1] * v[i] % MOD;
else f[i] = f[i - 1];
}
} int main()
{
get_p();
get_f(); int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n); printf("%lld\n", f[n + 1] * MIM(n + 1) % MOD);
} return 0;
}
HDU5407.CRB and Candies(数论)的更多相关文章
- HDU5407 CRB and Candies 【LCM递推】
HDU5407 CRB and Candies 题意: 计算\(LCM(C(n,0),C(n,1),C(n,2),\cdots,C(n,n-1),C(n,n))\) \(n\le 10^6\) 题解: ...
- ACM学习历程—HDU5407 CRB and Candies(数论)
Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...
- 【HDOJ 5407】 CRB and Candies (大犇推导
pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...
- HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- CRB and Candies LCM 性质
题目 CRB and Candies 题意 \[ \text{给定正整数N,求} LCM \lbrace C \left(N , 0 \right),C\left(N , 1 \right),..., ...
- Hdu 5407 CRB and Candies (找规律)
题目链接: Hdu 5407 CRB and Candies 题目描述: 给出一个数n,求lcm(C(n,0),C[n,1],C[n-2]......C[n][n-2],C[n][n-1],C[n][ ...
- 2015 Multi-University Training Contest 10 hdu 5407 CRB and Candies
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 数论 HDOJ 5407 CRB and Candies
题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1) ...
- CRB and Candies(组合数学+求逆元+lcm)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...
随机推荐
- BZOJ 1507 [NOI2003]Editor
Description Input 输 入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉 ...
- Spring中argNames的含义
最近学习Spring,一直不太明白Srping的切面编程中的的argNames的含义,经过学习研究后,终于明白,分享一下 先看一个例子: 需要监控的类: package bean; public cl ...
- Python 之 使用 PIL 库做图像处理
http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html Python 之 使用 PIL 库做图像处理 1. 简介. 图像 ...
- Java免费开源数据库、Java嵌入式数据库、Java内存数据库
Java免费开源数据库.Java嵌入式数据库.Java内存数据库 http://blog.csdn.net/leiyinsu/article/details/8597680
- 李洪强iOS开发之-环信05_EaseUI 使用指南
李洪强iOS开发之-环信05_EaseUI 使用指南 EaseUI 使用指南 简介 EaseUI 封装了 IM 功能常用的控件(如聊天会话.会话列表.联系人列表).旨在帮助开发者快速集成环信 SDK. ...
- [jobdu]扑克牌顺子
一开始看到题还以为要DFS还是BFS,后来发现完全不用.排个序,然后看看大小王能不能弥补缺口就行,但后来又发现还要排除有相同大小牌的情况. #include <iostream> #inc ...
- [jobdu]数组中的逆序对
http://ac.jobdu.com/problem.php?pid=1348 数组中的逆序对也是个常见的题目,算法导论中也有一些描述,参考:http://www.cnblogs.com/wuyue ...
- Protel封装库
一.目录下面的一些封装库中,根据元件的不同封装我们将其封装分为二大类:一类是分立元件的封装,一类是集成电路元件的封装 1.分立元件类: 电容:电容分普通电容和贴片电容: 普通电容在Miscellane ...
- RHEL/CentOS/Fedora常用的 CentOS 5/6/7 yum 源(EPEL、Remi、RPMForge、RPMFusion, ius,163,sohu,阿里云)配置
RHEL以及他的衍生发行版如CentOS.Scientific Linux为了稳定,官方的rpm repository提供的rpm包往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这是服务器版本,安 ...
- HDU4526威威猫系列故事——拼车记(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4526 额..七夕快乐哦 刚推的时候有点乱 又各种小错误 查了好久.. dp[i][k] = min(dp[i-1 ...