\[ \text{给定正整数N,求} LCM \lbrace C \left(N , 0 \right),C\left(N , 1 \right),...,C\left(N , N \right) \rbrace \% mod \qquad 1\leq N \leq 10^6
\]

  • 题解

    • 根据规律推出公式,另外关于这个公式的证明

      \[ LCM \lbrace C \left(N , 0 \right),C\left(N , 1 \right),...,C\left(N , N \right) \rbrace \quad = \quad \frac{LCM(1,2,3...,N+1)}{N+1}
      \]

    • 由于数据规模较大,除法取模可以化为求乘以N+1在模mod意义下的逆元再取模,问题就转化为了如何求连续自然数的LCM

    • 由唯一分解定理可知LCM算法:

    \[ X_1 = P_1^{e_1}*P_2^{e_2}*...*P_k^{e_k} \quad \\
    X_2 = P_1^{e_1'}*P_2^{e_2'}*...*P_k^{e_k'} \quad \\
    \text{$k$ $\to$ ∞ $\quad$ $P_i$ is a prime number} \\
    \text{}\\
    LCM(X_1,X2) = P_1^{max(e_1,e_1')} * P_2^{max(e_2,e_2')} *...* P_k^{max(e_k,e_k')}
    \]

    • 不妨设LCM[i]表示从1到i的lcm,则LCM[i+1]与LCM[i]的关系为

\[LCM \left[ i\right] =
\begin{cases}
LCM[i-1]*i\%mod, & \text{if $i$ is a prime number, because a new prime come in} \\[2ex]
LCM[i-1]*x\%mod, & \text{if $n$ is a POWER of prime number x, because $P_x$ get a high power }\\[2ex]
LCM[i-1], & \text{oherwise}
\end{cases}
\]

  • AC代码
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
const ll mod = 1e9+7;
ll LCM[maxn];
bool isPrime[maxn];//isPrime[i]表示i是不是质数
int prime[maxn];//prime[i]表示i是哪个质数的k次幂 如果不是某个质数的k次幂就置0
void init(){
for(int i=1; i<maxn; i++) isPrime[i] = 1, prime[i] = 0;
isPrime[1] = 0;
//素数普通筛 只需要筛到根号N即可
for(int i=2; i*i<maxn; i++){
if(isPrime[i]){
///cout<<i<<" ";
for(int j=i*i; j<maxn; j *= i){
prime[j] = i;//j 是 质数i的幂次
}
//筛素数
for(int j=i+i; j<maxn; j += i){
isPrime[j] = 0;
}
}
}
}
//LCM[i]表示(1 , 2 , 3 ......, i)的lcm
void getLCM(){
LCM[1] = 1;
for(int i=2; i<maxn; i++){
//如果是一个新质数
if(isPrime[i]) LCM[i] = LCM[i-1] * i % mod;
//如果不是质数 验证是不是一个质数的幂次
//如果是 那么说明遇到了一个质因子更高的幂次 那么要多乘一个这个质因子
else if(prime[i]) LCM[i] = LCM[i-1] * prime[i] % mod;
else LCM[i] = LCM[i-1];
} }
//扩展欧几里得求逆元
void extgcd(ll a,ll b,ll& d,ll& x,ll& y){
if(!b){ d=a; x=1; y=0;}
else{ extgcd(b,a%b,d,y,x); y-=x*(a/b); }
}
//返回a在模n意义下的逆元
ll inverse(ll a,ll n){
ll d,x,y;
extgcd(a,n,d,x,y);
return d==1?(x+n)%n:-1;
} int main(){
int t;
ll n;
init();
getLCM();
cin>>t;
while(t--){
cin>>n;
ll ans = LCM[n+1] * inverse(n+1 , mod) % mod;
cout<<ans<<endl; }
return 0;
}

CRB and Candies LCM 性质的更多相关文章

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

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

  2. 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\) 题解: ...

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

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

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

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

  6. 【HDOJ 5407】 CRB and Candies (大犇推导

    pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...

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

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

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

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

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

随机推荐

  1. RDD算子、RDD依赖关系

    RDD:弹性分布式数据集, 是分布式内存的一个抽象概念 RDD:1.一个分区的集合, 2.是计算每个分区的函数 ,    3.RDD之间有依赖关系 4.一个对于key-value的RDD的Partit ...

  2. MySQL基础7-分页查询

    1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...

  3. SXCPC2018 nucoj2004 国王的怪癖

    可持久化trie.考场上我脑补了一个trie树合并也A了 #include <iostream> #include <cstring> #include <cstdio& ...

  4. 【Word Break】cpp

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  5. IOS开发学习笔记042-UITableView总结2

    一.自定义非等高的cell         如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...

  6. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  7. thulac安装问题

    目标:在anaconda里面安装thulac. 1.打开Anaconda Prompt 2.输入pip install thulac 结果:报错! 报错画面如下: 解决方案: 1.到https://p ...

  8. [oldboy-django][2深入django]django模板使用函数

    1 模板引入子html--include 模板引擎 - 母版 - include,导入公共的html a. 用法:{% include "pub.html" %}, pub.htm ...

  9. jquery版tab切换效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. java 数据库连接 驱动相关参数

    mysql: driverClass=com.mysql.jdbc.Driver connectionURL=jdbc:mysql://localhost:3306/shiro userId=root ...