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

题意:求LCM(C(n,0),C(n,1),C(n,2),...,C(n,n)) (LCM指的是最小公倍数)

思路:一开始想每次求两个数的最小公倍数,然后求得n个数的最小公倍数,结果发现打表打不出= =。看了别人思路,发现求的式子是一个数学公式

令a[n]=LCM(C(n,0),C(n,1),C(n,2),...,C(n,n))
b[n]=LCM(1,2,3,...,n)
a[n]=b[n+1]/(n+1)
if(n=p^k) bn=p*bn-1 else bn=bn-2 p为素数,符合要求的n如4,8,9,25

所以我们可以先把素数筛选出来,并判断1~n这些数是不是等于p^k,把a[]数组预处理出来,然后用逆元就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
#define MOD 1000000007
int prime[maxn];
ll inv[maxn];
ll a[maxn];
void shake(){
int i;
inv[1]=1;
for(i=2;i<=1000000;i++){
inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
}
}
ll gcd(ll a,ll b){
return b ? gcd(b,a%b) : a;
}
int ok(int x)
{
int t=x,i,j;
while(t){
if(t%prime[x]==0)t/=prime[x];
else break;
}
if(t==1)return 1;
else return 0;
}
void init()
{
int i,j;
for(i=1;i<=1000000;i++)prime[i]=i;
for(i=2;i<=1000000;i++){
if(prime[i]==i){
for(j=i+i;j<=1000000;j+=i){
prime[j]=i;
}
}
}
a[1]=1;
for(i=2;i<=1000000;i++){
if(ok(i)){
a[i]=a[i-1]*prime[i]%MOD;
}
else a[i]=a[i-1];
}
}
int main()
{
int T,i,j;
ll n,m,ans,num;
shake();
init();
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
printf("%lld\n",a[n+1]*inv[n+1]%MOD );
}
return 0;
}

hdu5407CRB and Candies (逆元+数学公式)的更多相关文章

  1. HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】

    CRB and Candies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

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

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

  3. CRB and Candies(组合数学+求逆元+lcm)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...

  4. HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)

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

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

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

  6. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

  7. hdu 5407【LCM性质】+【逆元】(结论题)

    <题目链接> <转载于 >>> > Problem Description CRB has N different candies. He is going ...

  8. HDU 6050 17多校2 Funny Function(数学+乘法逆元)

    Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7.   ...

  9. hdu 5651 xiaoxin juju needs help 逆元 两种求解方式

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

随机推荐

  1. 【Flutter】功能型组件之导航返回拦截

    前言 为了避免用户误触返回按钮而导致APP退出,在很多APP中都拦截了用户点击返回键的按钮,然后进行一些防误触判断,比如当用户在某一个时间段内点击两次时,才会认为用户是要退出(而非误触).Flutte ...

  2. ORA-12560错误

    ora-12560错误是一个经典错误之一 下面我们分析一下这个错误: 产生这个错误的原因是什么呢? 1.oracle服务没有启动 Linux下查看$ps -ef | grep ora_ windows ...

  3. oracle查看用户的系统权限,角色以及数据库对象权限

    select * from dba_sys_privs where GRANTEE='monkey'; select * from dba_role_privs where GRANTEE='monk ...

  4. ABAP 面试问题和答案

    What is an ABAP data dictionary?- ABAP 4 data dictionary describes the logical structures of the obj ...

  5. Docker 建站小记

    一,前言 Docker 建站小记,我使用了四个镜像来搭建:nginx,certbot,mysql,gradle.欢迎访问:https://www.zzk0.top 这个网页是从 github 上找的个 ...

  6. UI测试框架

    1. 从上到下共分成4层: 用例层  组件管理层  元素管理层  公共数据层 2. 用例层: 将每条用例使用参数化, 公共参数存储到"公共数据层", 中间参数通过组件层传递 3. ...

  7. JS编写的科学计算器

    最近半个月编写了一个JS+CSS+HTML的网页计算器,从最初的具有简陋界面的简单计算器改版到最终具有科学/标准计算器转换功能并且界面非常友好的计算器,收获良多!总的来说,代码简单,通俗易读,下面贴上 ...

  8. FPGA仿真的概念及语法特点

    以下是特权同学<FPGA设计+实战演练>书中的描述:      一个正规的设计需要花费在验证上的工作量,往往可能会占到整个开发流程的70%左右.验证通常分为仿真验证和板机验证.      ...

  9. C语言中二维数组声明时,探究省略第一维的原因

    我们在使用二维数组作为参数时,我们既可以指明这个数组各个维度的维数,同时我们也可以省略一维,但是二维却不能省略.why呢?由于编译器原理的限制,在一个数组Elemtype test[m][n]中,访问 ...

  10. 你真的了解Android系统启动流程吗?Android高级工程师必看系列,已开源

    前言 从毕业到现在面试也就那么几家公司,单前几次都比较顺利,在面到第三家时都给到了我offer!前面两次找工作,没考虑到以后需要什么,自己的对未来的规划是什么,只要有份工作,工资符合自己的要求就行!所 ...