L Clock Master(分组背包)2020 China Collegiate Programming Contest Weihai Site

With the rapid development of society, the demand for high-precision clocks is constantly rising. Recently, the China Clock Production Company is developing a new type of clock, which can represent a wide range of times.

The novel clock displays the current time in an unusual fashion. The clock consists of several pointers, each controlled by a gear. All gears rotate synchronously – one tooth per period. However, the numbers of teeth of the gears may differ. If a gear has t teeth, then the corresponding pointer can point to t different directions, denoted 0,1,2,⋯,t−1, respectively, where 0 is the initial direction. Furthermore, if a clock is equipped with n pointers, the i-th of which is controlled by a t​i​​ -tooth gear, then the i-th pointer will point to k mod t​i​​ after k periods of time.

The price for a t-tooth gear is t yuan. Given a total budget of b yuan, you need to design a combination of gears, such that the number of valid combinations of directions of pointers is maximized, and the total cost on gears does not exceed the budget. A combination of directions (d​1​​ ,d​2​​ ,⋯,d​n​​ ) is valid, if it can be written for some nonnegative integer k, where t​i​​ is the number of teeth of the i-th gear. Since the answer may be too large, output the answer in natural logarithm (logarithm with base e=2.718281828⋯).
1<T<30000
1<b<30000

Sample Input:

3
2
7
10

Sample Output:

0.693147181
2.484906650
3.401197382

题目大意

T组数据,
给你一个n,让你找若干个数xi,使得x1+x2+…xi<=n,且lcm(x1,x2,…,xi)最大
输出log(lcm)

思路

对于一个数n,要使他的lcm最大,那么就要保证xi两两互质(这样才能没有浪费)
因此xi可以看成某个质数的p次方,且该质数只能选择一次,例如3和9不能同时选,因为这样无法保证互质
那么很显然,问题被抽象成了分组背包,每个质数为一组,组里为该质数的p次方,背包容量为n,每件物品的贡献为log(xi)

CODE

#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
#define MOD 1000000007
#define MAXN 30050
LL read()
{
	LL w = 1, x = 0;
	char ch = 0;
	while (ch < '0' || ch>'9')
	{
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	return w * x;
}
LL n, m, cnt, t, prime[MAXN], p[3333][200], b[3333];//prime记录素数 
double a[3333][200],dp[30050];
bool v[MAXN];
int main()
{
	n = 30005;
	v[0] = 1;
	v[1] = 1;
	for (register int i = 2; i <= n; i++)
	{
		if (!v[i])
		{
			prime[++cnt] = i;
		}
		for (register int j = 1; j <= cnt; j++)
		{
			if (prime[j] * i > n)
			{
				break;
			}
			v[prime[j] * i] = 1;
			if (i % prime[j] == 0)
			{
				break;
			}
		}
	}
	for (register int i = 1; i <= cnt; i++)
	{
		LL s = 1;
		for (register int j = 1; s <= 30005; j++)
		{
			s *= prime[i];
			a[i][j] = log(s);
			b[i] = j;
		}
	}
	for (register int i = 1; i <= cnt; i++)
	{
		LL s = 1;
		for (register int j = 1; s <= 30005; j++)
		{
			s *= prime[i];
			a[i][j] = log(s);
			b[i] = j;
			p[i][j] = s;
		}
	}
	int t = read();
	for (register int i = 1; i <= cnt; i++)
	{
		for (register int j = 30001; j >0; j--)
		{
			for (register int k = 1; k <= b[i]; k++)
			{
				if (j - p[i][k] >= 0)
					dp[j] = max(dp[j], dp[j - p[i][k]] + a[i][k]);
			}
			dp[j]=max(dp[j],dp[j-1]);
		}
	}
	while (t--)
	{
		n = read();
		printf("%.9lf\n",dp[n]);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值