2015多校联合第十场hdu5407CRB and Candies数论 唯一分解定理

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 ≤  K  ≤  N )?
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 ≤  N  ≤  106
 

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
 


当时做这个题的时候,感觉组合数都求不出来,当然了,求出来再取最小公倍数也超时了。搜题解 搜出来一个Krummer定理,这是什么鬼@。@

百度百科:设m,n为正整数,p为素数,则C(下m+n上m)含p的幂次等于m+n在p进制下的进位次数。

这个和这个题有什么关系呢?听我细细道来……这个题是要求C(n,k) (k从0到n)这n+1个数的最小公约数,那么,根据唯一分解定理可知,在这些数里,想找某质数的最大幂次,就

需要找到一个k使得进位的次数最多,当然了 k具体是什么我们不需要知道 ~_~ 但是我们需要根据n推测出进位的是哪位,既然是进了位的,那么这位在p进制下一定是小于k-1的,

后面的数都进位,那么才会是最大的幂次。

#include <iostream>
#include<cstdio>
#include<cstring>
#define N 1000004
using namespace std;
const int MOD = 1e9 + 7;
bool vis[N];
int prime[N/4],idx;
void init()
{
    idx=0;
    memset(vis,1,sizeof(vis));
    for(int i=2;i<N;i++)
    {
        if(vis[i])
        {
            prime[idx++]=i;
            for(int j=1;j*i<N;j++)
                vis[j*i]=0;
        }
    }
}
long long  pow_mod(long long a,int k)
{
    long long ans=1;//这里需要long long 毕竟10^9……
    while(k>0)
    {
        if(k&1) ans=ans*a%MOD;
        a=a*a%MOD;
        k>>=1;
    }
    return ans;
}
int get_pow(int n,int p)
{
    int tmp[30],ans=0;
    while(n>0)
    {
        tmp[ans++]=n%p;
        n/=p;
    }
    int flag=0,res=0;
    for(int i=0;i<ans-1;i++)
    {
        if(tmp[i]<p-1+flag)
        {
            flag=1;
            res++;
        }
    }
    return res;
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int t,n;
    long long ans;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%d",&n);
        ans=1;
        for(int i=0;i<idx&&prime[i]<=n;i++)
        {
            ans=(ans*pow_mod(prime[i],get_pow(n,prime[i])))%MOD;

        }
        printf("%I64d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值