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. maven 报的一堆错

    今天初学maven,刚开始下载的是Apache-maven-3.6.2然后配置运行一个servlet,但是在pom.xml中写jar包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...

  2. Redis-4.X 版本 Redis Cluster集群 (一)

    一 创建redis cluster 集群前提条件: 1 ) 每个redis node 节点采用相同的硬件配置,相同的密码. 2 ) 每个节点必须开启的参数: cluster-enabled yes # ...

  3. explain extended;show warnings

    mysql> explain extended select count(*) from xuehao;+----+-------------+-------+------+---------- ...

  4. Kubernetes K8S之kube-prometheus概述与部署

    Kubernetes K8S之kube-prometheus概述与部署 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7. ...

  5. LeetCode108.有序数组转二叉搜索树

    题目 1 class Solution { 2 public: 3 TreeNode* sortedArrayToBST(vector<int>& nums) { 4 if(num ...

  6. Nginx和Tomcat配置SSL实现https访问

    环境:CentOS 7 Nginx版本: nginx/1.18.0 1. 安装nginx 详细步骤可以参考如下官网:http://nginx.org/en/linux_packages.html#RH ...

  7. 并发编程常用工具类(二) SymaPhore实现线程池

    1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...

  8. Rancher On K3s 高可用架构部署

    Rancher 推荐部署架构 k3s 模式 RKE 和 k8s 模式 备注: 我对 RKE 的理解就是 Ansible + kubeadm 的打包,首先 rke 需要到每一个节点都可以免密 ssh , ...

  9. 【UNIAPP】接入导航系统完整版

    // 查询附近/搜索关键词 <template> <view> <!--地图容器--> <map id="myMap" :markers= ...

  10. 【转载】【GDB】GDB with Python

    作者:薛定谔的喵链接:https://zhuanlan.zhihu.com/p/152274203来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 你还在用GDB调试程序 ...