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, ...
随机推荐
- Android-BroadcastReceiver具体解释
什么是Broadcast Broadcast即广播,在Android广播是很重要的功能.比如我们想在系统开机之后做某些事情.监控手机的电量.监控手机的网络状态等等.这些功能都须要用到广播.当然我们也能 ...
- StarUML破解教程
StarUML破解教程 StarUML官方下载地址:http://staruml.io/download StarUML是一个非常好用的画UML图的工具,但是它是收费软件,以下是破解方法: 1.使用E ...
- C++ 错误积累
错误一 VS2012错误:不能在成员函数 的类外部重新声明该函数 解决:检查函数的大括号匹配
- Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks
Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks S. Chen, C. F. N. Cow ...
- iOS 蓝牙开发之(CoreBlueTooth)
CoreBlueTooth 简介: 可用于第三方的蓝牙交互设备 设备必须支持蓝牙4.0 iPhone的设备必须是4S或者更新 iPad设备必须是iPad mini或者更新 iOS的系统必须是iOS 6 ...
- ElasticSearch(二十六)修改分词器及定制自己的分词器
1.默认的分词器 standard 分词器 standard tokenizer:以单词边界进行切分standard token filter:什么都不做lowercase token filter: ...
- Mybatis sql注入问题
预编译方式,即PreparedStatement,可以防注入:#{id} <select id="getBlogById" resultType="Blog&quo ...
- spring mvc 基本原理
在web.xml配置spring mvc入口servlet: <servlet> <servlet-name>mvc-dispatcher</servlet-name&g ...
- activiti 基础搭建
首先在eclipse内添加activiti的插件,https://blog.csdn.net/qq_22701869/article/details/79537971 1.创建一个maven的java ...
- 改善程序与设计的55个具体做法 day5
条款12:复制对象时勿忘其每一个成分 这里的复制是拷贝构造和operator= 每一个成分有几个维度: 1.每个成员变量 这个很好理解,添加新的成员时也要记得为每个新添加的成员执行合适的复制操作 2. ...