hdu 5407(LCM好题+逆元)

CRB and Candies

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


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 ≤ KN)?
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 ≤ N106
 

 

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
 

 

Author
KUT(DPRK)
 
这个题我是知道(n+1)*LCM(C(n,0),C(n,1)..C(n,n)) = LCM(1,2,3..n) 这个公式的,但是用错了方法求(1-n)的LCM。。(也就是经常用的那个lcm(a,b) = a/gcd(a,b)*b)但是这个公式里面是 不允许取模的。。所以一直弄不出来。。然后在网上看到了很牛逼的公式。。数论真是很神奇啊。
我感觉这种题没接触过神牛也不一定能够解出来吧。。
还有一个人的题解提供了一种方法,不过没看懂,但是他提供了一种求解最小公倍数的方法。
---------------------------------------------------------------------------------------------------------------------------------------------------------------
分解质因数法:

先把这几个数分解质因数,再把它们一切公有的质因数和其中几个数公有的质因数以及每个数的独有的质因数全部连乘起来,所得的积就是它们的最小公倍数。

例如,求LCM[12,18,20,60]

因为12=(2)×[2]×[3],18=(2)×[3]×3,20=(2)×[2]×{5},60=(2)×[2]×[3]×{5}

其中四个数的公有的质因数为2(小括号中的数),

三个数的公有的质因数为2与3[中括号中的数],

两个数的公有的质因数为5{大括号中的数},

每个数独有的质因数为3。

所以,[12,18,20,60]=2×2×3×3×5=180。

 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = 1000005;
const LL mod = 1000000007;
LL f[N];
LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}

LL extend_gcd(LL a,LL b,LL &x,LL &y){
    if(!b){
        x=1,y = 0;
        return a;
    }else{
        LL x1,y1;
        LL d = extend_gcd(b,a%b,x1,y1);
        x = y1;
        y = x1 - a/b*y1;
        return d;
    }
}
LL mod_reverse(LL a,LL n)
{
    LL x,y;
    LL d=extend_gcd(a,n,x,y);
    if(d==1) return (x%n+n)%n;
    else return -1;
}
int prime[N];
LL F[N];
bool only_divide(int n){
    int t = prime[n];
    while(n%t==0){
        n/=t;
    }
    if(n==1) return true;
    return false;
}
void init(){
    for(int i=1;i<N;i++){
        prime[i] = i;
    }
    for(int i=2;i<N;i++){  ///十分巧妙的一步,判断某个数是否只有唯一的质因子,只需要把每个数的倍数存下来
        if(prime[i]==i){
            for(int j=i+i;j<N;j+=i){
                prime[j] = i;
            }
        }
    }
    F[1] = 1;
    for(int i=2;i<N;i++){
        if(only_divide(i)){
            F[i] = F[i-1]*prime[i]%mod;
        }else F[i] = F[i-1];
    }
}
int main()
{
    init();
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int n;
        scanf("%d",&n);
        LL inv = mod_reverse((n+1),mod);
        printf("%lld\n",F[n+1]*inv%mod);
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/liyinggang/p/5578654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值