HDU 2521 反素数(区间反素数)

52 篇文章 0 订阅

题目链接:
HDU 2521 反素数
题意:
[a,b]
分析:
先求 [1,b] 中约数最多的数 ans ,如果 ans[a,b] 那就暴力跑。本身这题数据也不大。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;

typedef unsigned long long lint;
const lint inf = ~0ull;
const int prime[]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};

int T, cnt;
int a, b;
lint ans;

void dfs(int depth, int limit, lint tmp, int num)
{
    if(tmp > b) return;
    if(num > cnt || (num == cnt && tmp < ans)) {
        ans = tmp;
        cnt = num;
    }
    for(int i = 1; i <= limit; ++i) { 
        if((double)tmp * prime[depth] > b) break; 
        tmp *= prime[depth];
        dfs(depth + 1, i, tmp, num * (i + 1));

    }
}

int main()
{
    scanf("%d", &T);
    while(T--) {
        cin >> a >> b;
        ans = inf, cnt = 0;
        dfs(0, 63, 1, 1);
        if(ans >= a && ans <= b) cout << ans << endl;
        else {
            cnt = 0;
            for(int i = a; i <= b; ++i) {
                int num = 0;
                for(int j = 1; j * j <= i; ++j) {
                    if(i % j == 0) {
                        if(i / j != j) num += 2;
                        else num++;
                    }
                }
                if(num > cnt) {
                    cnt = num; 
                    ans = i;
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值