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, ...
随机推荐
- 忘记glassfish密码,那就重置密码呗
方法一:如果现有的 domain 上并没有你所需要的东西,删除现有的 domain,重新创建一个 domain. 找到安装glassfish的目录下的 \bin\asadmin 目录,然后打开asad ...
- poj2421
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22048 Accepted: 93 ...
- IIS架构介绍
IIS7及以上版本提供的请求-处理架构包括以下内容: Windows Process Activation Service(WAS)可以让站点支持更多协议,不仅仅是HTTP和HTTPS 可以通过增加或 ...
- 学习Sharding JDBC 从入门到出门-02:源码揣测
sjdbc有读写分离的功能,要使用这个功能,在创建数据源对象是要使用类:MasterSlaveDataSource,并且设置主备数据源和数据库名称 这个对象有下面的属性: name:数据库的名称 ma ...
- tomcat 编码问题
默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: < Connector port="8080 ...
- HTML-Table-Td固定宽度使内容换行
table style="TABLE-LAYOUT: fixed;" td style="WORD-WRAP: break-word;WIDTH:200px;"
- 区分Web前端和后端(转载)
转载自:http://blog.csdn.net/rosetta/article/details/53871766 前言 做C开发将近六年,基本上没有接触过web相关的东西,原来听别人说web相关 ...
- Linux使用yum安装rpm包
1.yum其实管理的也是rpm包,只不过依赖什么的都自己做了2.yum在有的linux版本是收费的,但是CentOS是免费的3.yum一般意义上是需要联网的,即:使用网络yum源 a.yum源配置文件 ...
- Linux显示网络相关信息
netstat -tlun 查看本机监听的端口 netstat -an 查看本机所有的网络连接 netstat -rn 查看本机路由表 -t TCP协议 -u UDP协议 - ...
- 销售订单、外向交货单、交货 bapi
转自[http://www.cnblogs.com/elegantok/archive/2009/10/18/1585398.html]***********SALES ORDER INPUT CRE ...