\[ \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. 菜鸟学Linux - Linux文件属性

    在Linux中,文件的属性是一个很重要的概念,用户或者用户组对一个文件所拥有的权限,都可以从文件的属性得知. 我们可以通过ls -al命令,列出某个文件夹下面的所有文件(包括以.开头的隐藏文件).下面 ...

  2. Python 交互模式中 Delete/Backspace 键乱码问题

    进入 Python 交互模式,按下 Delete/Backspace 键,会出现 ^H 字符 解决方式: 1. 进到 Python 的Modules目录 [root@cyt-test Python-2 ...

  3. data相关应用

    文案参考:HTML5中的data-*属性和jQuery中的.data()方法使用 data属性选择器 $("li[data-id='1']")//选择li元素中data-id属性等 ...

  4. 极简Node教程-七天从小白变大神(二:中间件是核心)

    当我们只引入express时,前述的那些功能都是没有启用的.那么,如何将这些功能添加进来呢?express通过其中间件机制实现了这些功能的管理.每一个中间件对应一个功能,而中间件可以是第三方库,也可以 ...

  5. IOS开发学习笔记018- 一般控件的使用

    1.移动 2.动画 3.缩放 3.旋转 4.简化代码 5.总结 UIButton 的两种状态 normal highlighted  1.移动 OC语法规定:不允许直接修改某个对象中结构体属性的成员. ...

  6. Mybatis使用-Error attempting to get column 'type' from result set. / '255' in column '4' is outside valid range for the datatype TINYINT.

    一.遇到的问题是这样的: [RemoteTestNG] detected TestNG version 6.9.10log4j: Parsing for [root] with value=[DEBU ...

  7. [HTTPS]pfx转jks

    keytool -importkeystore -srckeystore  src.pfx -srcstoretype pkcs12 -destkeystore trg.jks -deststoret ...

  8. 整理 pycharm console调试博客

    在Debug模式下,查看变量发现只能看到300个变量,报错: two large to show contents. Max items to show:300. 点击Debugger左侧consol ...

  9. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

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

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