CRB and Candies LCM 性质
- 题目 CRB and Candies
- 题意
\]
题解
根据规律推出公式,另外关于这个公式的证明
\[ 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]的关系为
\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 性质的更多相关文章
- HDU 5407 CRB and Candies(LCM +最大素因子求逆元)
[题目链接]pid=5407">click here~~ [题目大意]求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 [思路]来图更直观: 这个究竟是怎样推出的.说实话 ...
- 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\) 题解: ...
- 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][ ...
- HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 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 ...
- 【HDOJ 5407】 CRB and Candies (大犇推导
pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...
- 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 ...
- CRB and Candies(组合数学+求逆元+lcm)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...
- hdu 5407【LCM性质】+【逆元】(结论题)
<题目链接> <转载于 >>> > Problem Description CRB has N different candies. He is going ...
随机推荐
- 实验一 查看CPU和内存,用机器指令和汇编指令编程
(1):使用debug,将下面的程序段写入内存,逐条执行,观察每条指令执行后,CPU中相关寄存器中内存的变化. 机器码 汇编指令 b8 20 4e mov ax,4E20H 05 ...
- VS2010Datatable查看器查看超时(Microsoft.VisualStudio.DebuggerVisualizers)
这个问题由来已久,却一直没有找到原因.大家都知道,VisualStudio的DebuggerVisualizers是一个非常方便的插件,可以帮助我们调试时查看Datatable视图,前阵子突然发现在查 ...
- 运维Python面试题
本章内容 1 osodjfoskjdofjsdjfjsdf 123 sdfdfadf 1 2 3 4 5 6 from django.db import models class user ...
- Java中Scanner中nextLine()方法和next()方法的区别
https://blog.csdn.net/hello_word2/article/details/54895106
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- python学习-- Django REST framework 序列化数据操作
一.为什么要返回json数据? 一般来说前端要用到从后台返回的数据来渲染页面的时候,这时候后台就需要向前端返回json类型的数据,简单直观便于理解 ,就类似于 {"xxx":{[& ...
- [转]手写数字识别错误NameError: name 'mnist' is not defined
转自:https://blog.csdn.net/coder_Gray/article/details/78562382 在Tensorflow上进行mnist数字识别实例时,出现如下错误 NameE ...
- ALPHA 冲刺(一)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...
- 【转】UGUI VS NGUI
原文:http://gad.qq.com/college/articledetail/7191053 注[1]:该比较是基于15年-16年期间使用NGUI(3.8.0版本)与UGUI(4.6.9版本) ...
- .Net MVC删除图片
还在学校,菜鸟级别,接触到的只是 /// <summary> /// 根据imageID删除图片 /// </summary> /// <returns>< ...