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 ...
随机推荐
- vi 在行首尾添加字符串
在行首尾添加字符串 用下列命令在文本每行的行首添加字符串“NewString”,请注意空格的存在. :g/^/s//NewString/g :%s/^/NewString/g ...
- 简单3d RPG游戏 之 001 生命条(一)
1.创建一个新项目,引用如下的包: 2.将asset里的First Person Controller拖入project作为游戏角色,将其命名为Player,将mainCamera删除,这样就是用Pl ...
- ExtJS4.2学习(17)表单基本输入控件Ext.form.Field(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-11/189.html --------------- ...
- java对xml文件做增删改查
http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html http://www.blogjava.net/weishuan ...
- 今日网站突然报错,mysql的故障
Access denied for user 'root'@'localhost' (using password: YES) 错误位置 FILE: /var/www/html/ThinkPHP/Li ...
- Linux下eclipse的安装以及配置
在安装好jdk并配置好后,就可以进行eclipse的安装了,其步骤如下: 1.下载eclipse 我所用的eclipse为:eclipse-dsl-juno-SR1-linux-gtk.tar 2. ...
- Oracle DB优化
http://www.jb51.net/article/77876.htm http://www.jb51.net/article/56881.htm http://danni505.blog.51c ...
- android 在activity中改变标题栏的标题 tabActivity的标题改变
在activity中改变标题栏的标题是调用setTitle()方法,参数为标题名称. 而tabActivity跟Activity是一样的,因此在onCheckedChanged()方法中要动态改变标题 ...
- Web.xml配置详解之context-param(转)
本文转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6759206 格式定义: <context-param> <pa ...
- char 与 unsigned char的本质区别
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...