nefu119组合素数

/*
    题目描述:给出正整数n和素数p(1<n, p<1e9),问C(2*n , n)能整除p多少次
    
    方法:C(2*n , n) = (2*n)!/(n! * n!),因此答案就是2*n分解后p的指数减去2倍n分解后p的指数
             根据定理,n!分解后p的指数为 k = [n / p] + [n / p^2] + [n / p^3] +... + 0
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-8;
LL factor_times(LL n , LL p)
{
    LL temp = p , sum = 0 ;
    while( n / temp >0){
        sum += n / temp;
        temp *= p;
    }
    return sum;
}
int main()
{
    int T;
    LL n,p;
    scanf("%d",&T);
    while(T--){
        scanf("%lld %lld",&n , &p);
        LL ans1 = factor_times(2 * n , p);
        LL ans2 = factor_times(n , p);
        printf("%lld\n",ans1 - 2 * ans2);
    }
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值