CRB and Candies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 722    Accepted Submission(s): 361

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
 
Author
KUT(DPRK)
 
Source

题目大意:让你求LCM(C(n,0),C(n,1),C(n,2)...C(n,n-1),C(n,n)),最后结果取模。

解题思路:其实只要有公式了,问题就很好解决了。f(n)是求1 - n的最小公倍数。这个是可以借鉴得。如果n是一个素数p的k次方,那么就乘以素数p。主要需要求逆元,和快速判断x是否为素数p的k次方。

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=1e6+20;
const INT MOD=1e9+7;
INT f[maxn],g[maxn],inv[maxn];
int p[maxn];
void init(){
for(int i=1;i<maxn;i++){
p[i]=i;
}
for(int i=2;i<maxn;i++){
if(p[i]==i){
for(int j=i+i;j<maxn;j+=i){
p[j]=i;
}
}
}
}
bool check(int x){
int d=p[x];
if(x>1){
while(x%d==0){
x/=d;
}
return x==1;
}
return false;
}
void get_f(){
f[1]=1;
for(int i=2;i<maxn;i++){
if(check(i)){
f[i]=f[i-1]*p[i]%MOD;
}else{
f[i]=f[i-1];
}
}
}
INT Powmod(INT a,INT n){
a%=MOD;
INT ret=1;
while(n){
if(n&1)
ret= ret * a % MOD;
n>>=1;
a = (a*a)%MOD;
}
return ret;
}
INT get_inv(int n){
return Powmod((INT)n,MOD-2);
}
INT get_g(int n){
return f[n+1]*get_inv(n+1)%MOD;
}
int main(){
int t,n;
init();
get_f();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
INT ans=get_g(n);
printf("%lld\n",ans);
}
return 0;
}

  

HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】的更多相关文章

  1. 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][ ...

  2. 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 ...

  3. hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)2015 Multi-University Training Contest 10

    题意: 输入n,求c(n,0)到c(n,n)的所有组合数的最小公倍数. 输入: 首行输入整数t,表示共有t组测试样例. 每组测试样例包含一个正整数n(1<=n<=1e6). 输出: 输出结 ...

  4. HDU 5407 CRB and Candies(LCM +最大素因子求逆元)

    [题目链接]pid=5407">click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的.说实话 ...

  5. LCM性质 + 组合数 - HDU 5407 CRB and Candies

    CRB and Candies Problem's Link Mean: 给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n<=1e6). analy ...

  6. HDU 5407 CRB and Candies

    题意:给一个正整数k,求lcm((k, 0), (k, 1), ..., (k, k)) 解法:在oeis上查了这个序列,得知答案即为lcm(1, 2, ..., k + 1) / (k + 1),而 ...

  7. hdu 5407(LCM好题+逆元)

    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. HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)

    题目地址:pid=5407">HDU 5407 题意:CRB有n颗不同的糖果,如今他要吃掉k颗(0<=k<=n),问k取0~n的方案数的最小公倍数是多少. 思路:首先做这道 ...

随机推荐

  1. 【C#】 创建和调用webapi

    二,,通过普通的路由调用,,路径写到http://localhost:29920/api/Players  即   Api/controller  为止

  2. Docker-教程(一)CentOS Docker 安装

    Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发行版本中的内核支持 Docker. ...

  3. P3768 简单的数学题 杜教筛+推式子

    \(\color{#0066ff}{ 题目描述 }\) 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ij ...

  4. USACO 1.1.2 Greedy Gift Givers(gift1)

    这道题大意就是几个人互送礼物,让你求每个人的盈利. 原题给的样例数据: 5(人的个数.) =========(下面是人名,输出按照这顺序)davelauraowenvickamr ========== ...

  5. s5pv210移植Minigui3.0.12

    移植平台:ubuntu:14.04 开发板:s5pv210(A8) Minigui版本:3.0.12-------------------------------------------------- ...

  6. Laplace(拉普拉斯)算子

    [摘要] Laplace算子作为边缘检测之一,和Sobel算子一样也是工程数学中常用的一种积分变换,属于空间锐化滤波操作.拉普拉斯算子(Laplace Operator)是n维欧几里德空间中的一个二阶 ...

  7. 16.Shortest Unsorted Continuous Subarray(最短未排序子数组)

    Level:   Easy 题目描述: Given an integer array, you need to find one continuous subarray that if you onl ...

  8. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall

    题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...

  9. kvm重新命名

    1.停止虚拟机 virsh shutdown wcltest3 2.导出虚拟机的配置文件 cd /etc/libvirt/qemu virsh dumpxml wcltest3 > vnc.xm ...

  10. java中\r ,\n,\t等的含义

    \t 相当于tab,缩进,制表符\n NewLine 换行 \f 换页符 \r 回车 \" 转义 “\\ 转义 \