Problem Description

CRB has N different candies. He is going to eat K candies.
He wonders how many combinations he can select.
Can you answer his question for all K (0 ≤ K ≤ N )?
CRB is too hungry to check all of your answers one by one, so he only asks least common multiple(LCM) of all answers.

Input

There are multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case there is one line containing a single integer N .
1 ≤ T ≤ 300
1 ≤ N ≤ 106

Output

For each test case, output a single integer – LCM modulo 1000000007(109+7 ).

Sample Input

5

1

2

3

4

5

Sample Output

1

2

3

12

10

题目要求的是所有C(n, i) (0 <= i <= n)的最小公倍数。

这题如果直接用LCM去求会T掉,

就算离线所有n!的逆元复杂度是O(n)

然后for循环C(n, i)是O(n)

然后用LCM求最小公倍数是O(log(a)),最差情况接近O(log(10^9+7)) ~ 30

所以复杂度最差是O(30n), 300组数据,最终需要O(10^10)左右。

那个30不乘的话O(3*10^8)左右。卡了一个常数倍数量级。

max(Vp(C(n, i))) = max(Vp(i+1)) - Vp(n+1) (0 <= i <= n)

有了这个式子,就证明了[C(n, 0), C(n, 1) ,.....C(n, n)] = [1, 2, ....,n+1]/(n+1)

等式两侧的质因子指数相等,自然等式就相等了。

然后最终结果是所有p^maxN/p^k的乘积(其中maxN是p在n+1内的最高次数,k是p能整除n+1的最高次数)。

也就是所有p^maxN的乘积除以p^k的乘积,分子等于[1, 2, 3,....n+1],分母等于n+1。

这个结果和题解的结论是一致的。

证明过程有点搓。。。。

如果顺序找到k和maxN的话,复杂度是O(num*log(p)),其中num是素数个数,p是素数。

如果二分查找的话,是O(num*log(logp))

此外托人找了另一种证明方式,很巧妙:

代码:O(num*log(p))

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; const LL MOD = 1e9+;
const int maxN = 1e6+;
bool isprim[maxN];
int n, prim[maxN], top; //埃氏筛法求素数
void isPrim()
{
memset(isprim, true, sizeof(isprim));
isprim[] = isprim[] = false;//初始化
for (LL i = ; i < maxN; ++i)//筛法
{
if (isprim[i])
{
for (LL j = i*i; j < maxN; j += i)//上界太大可能会爆int
{
isprim[j] = false;
}
}
}
} void init()
{
isPrim();
top = ;
for (int i = ; i < maxN; ++i)
if (isprim[i])
prim[top++] = i;
} void work()
{
LL ans = ;
for (int i = ; i < top && prim[i] <= n+; ++i)
{
for (LL v = prim[i]; v <= n+; v *= prim[i])
{
if ((n+)%v)
ans = (ans*prim[i])%MOD;
}
}
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
scanf("%d", &n);
work();
}
return ;
}

ACM学习历程—HDU5407 CRB and Candies(数论)的更多相关文章

  1. ACM学习历程—HDU5410 CRB and His Birthday(动态规划)

    Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...

  2. ACM学习历程—BZOJ2956 模积和(数论)

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  3. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  4. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

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

  6. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  7. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  8. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  9. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

随机推荐

  1. mysql数据库的导出与导入

    导出 在dos节目,切换到mysql依照文件夹的bin下.输入下面命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后.就能够看到在bin ...

  2. Aeroplane chess(简单概率dp)

    Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz start ...

  3. Sql注入_类型

    1.sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2.sql注入类型 按照注入点类型来分类 (1)数字型注入点 在 Web ...

  4. Powerdesigner Name与Comment的互相转换

    使用说明: 在[Tools]-[Execute Commands]-[Edit/Run Script] 下.输入下面你要选择的语句即可: 1.Name填充Comment '把pd中那么name想自动添 ...

  5. [luogu4755]Beautiful Pair

    [luogu4755]Beautiful Pair luogu 第一次写最大值分治感觉有点丑 每次找到最大值mid,扫小的一边,主席树查大的一边小于等于\(\frac{a[mid]}{a[i]}\)的 ...

  6. [IOI2018]组合动作

    IOI2018 组合动作 UOJ 首先显然可以两次试出首字母 考虑增量构造 假设首字母为A,且已经试出前i个字母得到的串s 我们考虑press这样一个串s+BB+s+BX+s+BY+s+XA 首先这个 ...

  7. Linux软件包分类

    源代码包 优点: 1.给你的就是源代码 2.可以修改源代码 3.可以自由选择所需的功能 4.软件是在自己电脑上编译安装,所以更加稳定高效 5.卸载方便(直接删了你安装软件的那个目录就好了) 缺点: 1 ...

  8. Jquery点击事件出发顺序

    鼠标点击触发事件执行顺序: mouse down -> mouse up -> click 键盘点击出发事件执行顺序: 点击后马上抬起:key down -> key press - ...

  9. bug-2——tab中beforeActivate:在对象活动前触发

    $j("#tabs").tabs({ beforeActivate:function(event,ui){ var ret = apoCheck('${requestScope.a ...

  10. 阿里云修改centos7主机名

    为了玩Docker,买个阿里云主机也是够拼的了. [root@iZ284olvkmjZ ~]# 不过主机名中怎么好DT,无奈,修改. 我们需要的是永久生效,阿里云提供了两种方法: 方法(1). 输入h ...