转载:HDOJ 5407 CRB and Candies(求1到n的LCM 位图储存结构)

版权声明:本文为转载文章,未经博主允许不得转载。 https://blog.csdn.net/whyorwhnt/article/details/9397289

CRB and Candies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1056    Accepted Submission(s): 508


Problem Description
CRB has different candies. He is going to eat candies.
He wonders how many combinations he can select.
Can you answer his question for all (0 ≤ ≤ )?
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, indicating the number of test cases. For each test case there is one line containing a single integer.
1 ≤ ≤ 300
1 ≤ ≤ 
 
Output
For each test case, output a single integer – LCM modulo 1000000007().
 
Sample Input
 
 
5 1 2 3 4 5
 
Sample Output
 
 
1 2 3 12 10
 
Author
KUT(DPRK)
 
Source

题意:

求1到n的最小公倍数..然后网上给出了这样的一个递推公式...

要求C(0,n),C(1,n),。。。,C(n,n)的最小公倍数最终可化简为求LCM(1,2,3,...,n,n+1)/(n+1)。求LCM时,只有碰到质数或质数的多次幂时最小公倍数才会改变。。。(可以发现随着n的增长好多最小公倍数都是不变的,增长的位置都发生在有新的质因子产生或者原本质因子的次数增大的地方,所以首先找出1-1000的所有质数,然后把1-1000000的所有的质数幂统计出来。然后lcm就求出来了,最后乘以(n+1)的逆元即可)...但是如果要求模2^32的话程序就会自己爆掉了...不会写。。。

 //特别喜欢这个筛素数的写法,成功搞定了n的最大的质因子,同时判断了他是否为                                      a perfect power number    666啊

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdlib>
using namespace std;
const int N=1e6+10;
const int MOD=1e9+7;
typedef long long LL;
LL p[N];
LL arr[N];
bool ok(int n) //判断n是不是只有一个质因子,p[n]表示n最大的质因子。
{
    int t=p[n];
    while(n%t==0&&n>1) n/=t;
    return n==1;
}
LL poww(LL a,LL b)
{
    LL res=a,ans=1;
    while(b)
    {
        if(b&1) ans=res*ans%MOD;
        res=res*res%MOD;
        b>>=1;
    }
    return ans;
}
LL niyuan(LL a) /// 求逆元
{
    return poww(a,MOD-2);
}
inline LL read()
{
    int c=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    return c*f;
}
void init()
{
    for(int i=1; i<N; ++i) p[i]=i;
    for(int i=2; i<N; ++i) if(p[i]==i)
    {
        for(int j=i+i; j<N; j+=i)
            p[j]=i;
    }
    arr[0]=1;
    for(int i=1; i<N; ++i)//求LCM
    {
        if(ok(i))
            arr[i]=arr[i-1]*p[i]%MOD;
        else arr[i]=arr[i-1];
    }
}
int main()
{
   init();
   int t;t=read();
   while(t--)
   {
       int n;n=read();
       LL ans=arr[n+1]*niyuan(n+1)%MOD;//由欧拉定理a^(p-1) mod p = 1 p是质数 所以a的逆元是a^{p-2}
       printf("%lld\n",ans);
   } return 0;
}
View Code

贴另一个模2^32的题...用了一种高深的叫做 位图 的数据储存结构...Orz         LightOJ 1289 LCM from 1 to n 

猜你喜欢

转载自www.cnblogs.com/vainglory/p/9066690.html
crb