官方题解:

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(数论)的更多相关文章

  1. 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\) 题解: ...

  2. ACM学习历程—HDU5407 CRB and Candies(数论)

    Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...

  3. 【HDOJ 5407】 CRB and Candies (大犇推导

    pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...

  4. HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】

    CRB and Candies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. CRB and Candies LCM 性质

    题目 CRB and Candies 题意 \[ \text{给定正整数N,求} LCM \lbrace C \left(N , 0 \right),C\left(N , 1 \right),..., ...

  6. 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][ ...

  7. 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 ...

  8. 数论 HDOJ 5407 CRB and Candies

    题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1) ...

  9. CRB and Candies(组合数学+求逆元+lcm)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...

随机推荐

  1. OpenCV学习-b

    OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/modu ...

  2. MyBatis之传入参数——parameterType(转)

    鸣谢:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 ------------------------------------ ...

  3. [转载]MongoDB 常用命令

    mongodb由C++编写,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, o ...

  4. Oracle----date

    to_date('december 31, 2008, 11:30 p.m.', 'Month dd, YYYY, HH:MI P.M.')

  5. Es索引优化

    https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html https://www.elastic.co/gui ...

  6. IDEA如何打包可运行jar的一个问题。

    转自http://bglmmz.iteye.com/blog/2058785 背景: 有时候,我们会用IDEA来开发一些小工具,需要打成可运行的JAR包:或者某些项目不是WEB应用,纯粹是后台应用,发 ...

  7. *[codility]StoneWall

    https://codility.com/demo/take-sample-test/stone_wall 拼石块.用最少的方块.一开始想了想用贪心,是可行的,就是尽量每次把当前的高度往右延伸到最多, ...

  8. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词

    一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...

  9. QVariant实质 (类似 C#中的装箱拆箱)

    QVariant是一种可以存储不同类型的数据结构,在很多场合这是很有用得为了达到这种目的,可以想象,该对象应该存储对象的类型信息,数据信息以及其他辅助详细考虑用途,这种对象必须支持对不同对象的存储,对 ...

  10. Android 动态背景的实现以及SurfaceView中添加EditText控件

    首先还是一贯作风,我们先看案例: \ 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并不复杂,这个如果让做 ...