ACM学习历程—HDU5407 CRB and Candies(数论)
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(数论)的更多相关文章
- ACM学习历程—HDU5410 CRB and His Birthday(动态规划)
Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...
- ACM学习历程—BZOJ2956 模积和(数论)
Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
- ACM学习历程—HDU 5317 RGCDQ (数论)
Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...
- 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学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5666 Segment(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
随机推荐
- poj2349
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 0 Accepted: 0 Descript ...
- 【BZOJ3302】[Shoi2005]树的双中心 DFS
[BZOJ3302][Shoi2005]树的双中心 Description Input 第一行为N,1<N<=50000,表示树的节点数目,树的节点从1到N编号.接下来N-1行,每行两个整 ...
- jquery 访问后台方法 并且获取后方法返回的数据
说明: 1.开发环境 asp.net MVC4 c#语言. 后台方法位于控制器中ProController.cs中 后台方法如下: public string GetNumber() { string ...
- Netty 高并发 (长文)
目录 Netty+Zookeeper 亿级 高并发实战 (长文) 写在前面 1. 高并发IM架构与部分实现 1.1. 高并发的学习和应用价值 1.1.1. 高并发IM实战的价值 1.1.2. 高并发I ...
- 低秩近似 low-rank approximation
- Android Studio 1.1 使用介绍及导入 jar 包和第三方依赖库
导入 jar 包 导入 jar 包的方式非常简单,就是在项目中的 libs 中放入你需要导入的 jar 包,然后右键你的 jar 文件,选择“add as a library”即可在你的项目中使用这个 ...
- mysql主从复制(windows下)
简单搭建mysql主从服务器(双向复制),仅供学习之用. 1.下载mysql-advanced-5.6.16-winx64.zip 下载完成后解压,重命名成mysql,分别放入两台服务器(看第二点集群 ...
- vim 真是上瘾啊
再次更新 .vimrc " leaderlet mapleader = ","nnoremap <leader>ev :vsplit ~/.vimrc< ...
- LeetCode:二叉树的前、中、后序遍历
描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...
- hbase shell-namespace(命名空间指令)
hbase shell命名空间namespace篇: 1. alter_namespace (一般用于添加删除一个属性,很少使用) hbase(main)::> help 'alter_name ...