HDU 3641 Treasure Hunting(阶乘素因子分解+二分)

题目链接:传送门

题意:

求最小的 ( x! ) = 0 mod (a1^b1*a2^b2...an^bn)

分析:

首先吧a1~an进行素因子分解,然后统计下每个质因子的指数,由于随着x的增大,质因子的个数是逐渐增加的

因此我们可以二分x,对x!进行素因子分解判断是否满足条件,然后求出最小的就可以了。

代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 110;
typedef long long LL;
bool vis[maxn];
int p[maxn],cnt;
LL a[maxn];
LL b[maxn];
LL num[maxn];

void init(){
    cnt = 0;
    memset(vis,0,sizeof(vis));
    for(int i=2;i<maxn;i++){
        if(!vis[i]){
            p[cnt++]=i;
            for(int j=i+i;j<maxn;j+=i)
                vis[j]=1;
        }
    }
}

LL get_num(LL x,int pri){
    if(x<pri) return 0;
    return get_num(x/pri,pri)+(LL)x/pri;
}

bool check(LL x){
    for(int i=0;i<cnt;i++){
        if(get_num(x,p[i])<num[p[i]])
            return false;
    }
    return true;
}

int main()
{
    init();
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++){
            scanf("%I64d%I64d",a+i,b+i);
            int tmp = a[i];
            for(int j=0;p[j]*p[j]<=tmp&&j<cnt;j++){
                if(tmp%p[j]==0){
                    LL tot=0;
                    while(tmp%p[j]==0) tmp=tmp/p[j],tot++;
                    num[p[j]]+=tot*b[i];
                }
            }
            if(tmp>1) num[tmp]+=b[i];
        }
        LL ans = 0;
        for(int i=0;i<maxn;i++)
            ans=max(ans,(LL)i*num[i]);
        LL l=0,r=ans;
        while(l<=r){
            LL mid=(l+r)>>1;
            if(check(mid)) r=mid-1;
            else l=mid+1;
        }
        printf("%I64d\n",l);
    }
    return 0;
}
/*
111
6
6 1000000000000
15 1000000000000
13 1000000000000
7 1000000000000
2 1000000000000
3 1000000000000
*/


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值