官方题解:

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. 一步步学习NHibernate(4)——多对一,一对多,懒加载(1)

    请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们学会如何使用NHibernate对数据的简单查询,删除,更新和插入,那么如果说仅仅是这样的话,那么NHi ...

  2. wordpress mobile templates

    http://themeforest.net/category/wordpress/mobile http://themeforest.net/item/monolith-wp-theme-for-b ...

  3. 使用WampServer 3.0

    在server上安装了WampServer 发现本地使用良好,但是无法从别的PC访问. 原因有二: 1.现象:输入连接无反应 原因:server本身用了80端口,所有WampServer我就设置了80 ...

  4. jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据(转)

     jersey post提交到 ContainerRequestFilter 而HttpServletRequest获取不到数据 问题:在serverfilter request获取不到post提交的 ...

  5. Hbase region 某个regionserver挂掉后的处理

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwoAAACdCAMAAAAjbX91AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK

  6. POJ 1151 Atlantis(离散化)

    点我看题目 题意 : 就是给你n个矩形的最左下角和最右上角的点的坐标,然后将这n个矩形的面积求出来. 思路 : 离散化求矩形面积并.离散化具体介绍.将横纵坐标离散开来分别存,然后排序,也可以按照黑书上 ...

  7. SEO 网站页面SEO优化之页面title标题优化

    在seo优化中,标题的优化占着举足轻重的地位,无论是从用户体验的角度出发,还是从搜索引擎的排名效果出发,title标题都是页面优化最最重要的因素.笔者总结了优化title标题应该注意的六个方面: ①. ...

  8. *[topcoder]LittleElephantAndIntervalsDiv1

    http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...

  9. ANDROID_MARS学习笔记_S01原始版_008_Looper\Bundle异步消息处理

    一.流程 1.自定义Handler,重写handleMessage(Message msg),用msg得到bundle,从而得到传递过来的数据 2.开启android.os.HandlerThread ...

  10. 应付配置文件 Profile

    (N) System Administrator > Profile > System Profile Option Name Site Application Responsibilit ...