HDU 5878 I Count Two Three 青岛网络赛

本文介绍了一种算法,用于寻找大于等于给定数值n的最小的可以表示为2的a次方乘以3的b次方乘以5的c次方乘以7的d次方的数。通过预先计算所有可能的组合并进行排序,再使用二分查找来快速找到满足条件的最小数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意找出大于等于 n 的最小的 能表示为 2的a次 乘以 3的b次 乘以 5的c次 乘以 7的d次 的数。

n最大为1e9,符合条件的数也没几个,预处理出来,排个序,二分一下就好了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
using namespace std;
typedef long long LL;

const LL MAX = 1000000000;

LL ans[20000];
int cnt = 0;
LL Power(LL a, LL b)
{
	LL ans = 1;
	while (b > 0)
	{
		if (b&1)
			ans *= a;
		b >>= 1;
		a *= a;
	}
	return ans;
}

void init()
{
	LL tmp;
	for(int a = 0; a <= 31; a++)
	{
		for(int b = 0; b <= 19; b++)
		{
			for(int c = 0; c <= 12; c++)
			{
				for(int d = 0; d <= 11; d ++)
				{
					tmp = Power(2, a)*Power(3, b);
					if(tmp > MAX) break;
					tmp *= Power(5, c);
					if(tmp > MAX) break;
					tmp *= Power(7, d);
					if(tmp > MAX) break;
					ans[++cnt] = tmp;
				}
			}
		}
	}
}


int bisearch(int x)
{
	if(x == 1) return 1;
	int l = 1, r = cnt + 1, m;
	while(l < r)
	{
		m = (l + r) >> 1;
		if(ans[m] > x) r = m;
		else if(ans[m] == x) return ans[m];
		else l = m + 1;
	}
	return ans[l];
}

int main()
{
	init();
	sort(ans + 1, ans + 1 + cnt);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int tmp;
		scanf("%d", &tmp);
		printf("%d\n", bisearch(tmp));
	}
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值