官方题解:

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

    [转]Visual Studio.net 2013 asp.net MVC 5 Scaffolding代码生成向导开源项目 提高开发效率,规范代码编写,最好的方式就是使用简单的设计模式(MVC , R ...

  2. Random.Next获取随即数

    Random random = new Random(); random.Next()--------------返回非负的一个随机数. random.Next(int  maxValue)----- ...

  3. MVC-Model数据注解(三)-Remote验证的一个注意事项

    首先,一般来说对于一个属性的验证可能需要不止一个的远程验证,比如对于用户名来说,我们需要对于它的长度做一些限制,这个可以通过StringLength特性来解决:同时还需要验证用户名不能重复,这个就需要 ...

  4. js中的ajax的运用

    XMLHttpRequest对象 IE7+,FireFox,Chrome,Opera,Safari创建XHR对象: var xhr=new XMLHttpRequest(); 创建XHR对象的兼容性写 ...

  5. Emule使用Upnp,解决Lowid和port not reachable的问题

    路由器上钩选开启Upnp Emule->选择->扩展选项->Upnp, 服务器:[从URL更新]http://upd.emule-security.org/server.met

  6. eclipse中mavean的使用配置

    eclipse-jee-neon-R-win32 maven-3.3.9 JDK  jdk-8u101-windows-i586 eclipse中配置mavean的步骤就不说了,网上很多教程,也很简单 ...

  7. Winform datagridview相关操作

    datagridview显示行号的2种方法: 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: privatevoiddat ...

  8. 用Firefly创建第一个工程

    原地址:http://blog.csdn.net/uxqclm/article/details/10382097 安装完成之后,在python script包中就存在 firefly-admin的工具 ...

  9. (重)POJ 3020Antenna Placement

    http://poj.org/problem?id=3020 呃...这个题不是很会,所以找了大神的博客做了参考,说得很详细 http://blog.csdn.net/lyy289065406/art ...

  10. 玩玩EXPRESSJS

    呵呵,反正有的是学习时间哈哈, 这个EXPRESSJS,看看实现的大约. 看的是http://www.expressjs.com.cn/ 代码A: var express = require('exp ...