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 ti -tooth gear, then the i-th pointer will point to k mod ti 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 (d1 ,d2 ,⋯,dn ) is valid, if it can be written for some nonnegative integer k, where ti 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;
}