2020 CCPC 威海站 L Clock Master (数论+分组背包)

L Clock Master

Description

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 0,1,2,⋯,t−1 0,1,2,,t1, 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 kmodt​i​​ after k periods of time.

The price for a t-tooth gear is t yuan. Given a total budget of b b 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 d1,d2,⋯,dn d1,d2,,dn) is valid, if it can be written for some nonnegative integer k k 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 ⋯ e=2.718281828⋯ e=2.718281828).
Input Specification:

The first line of input is a single integer T ( 1 ≤ T ≤ 30000 ) T (1≤T≤30000) T(1T30000), indicating the number of test cases. Each test case is a single line of an integer b ( 1 ≤ b ≤ 30000 ) b (1≤b≤30000) b(1b30000), denoting the total budget.
Output Specification:

For each test case, print the natural logarithm, within an absolute or relative error of no more than 1 0 − 6 10^{-6} 106​​, of the maximum number of valid combinations, in a single line.

Sample Input 1:


3
2
7
10

Sample Output:

0.693147181
2.484906650
3.401197382

Notes:

For the second sample data, a 3 3 3-tooth gear along with a 4 4 4-tooth gear may yield 12 12 12 different combinations of directions, with total cost exactly being 7 7 7. So you should print the value of ln12, which is approximately 2.484906650 2.484906650 2.484906650.

分析

题目要求可以提炼为:给定一个数 n ( 1 ≤ n ≤ 30000 ) n(1 \le n \le 30000) n(1n30000),令 n = a 1 + a 2 + a 3 + . . . . n=a_1+a_2+a_3+.... n=a1+a2+a3+....,求 l c m ( a 1 , a 2 , a 3 , . . . . . ) lcm(a_1,a_2,a_3,.....) lcm(a1,a2,a3,.....)的最大值。

进一步思考可以想到,要使 l c m lcm lcm最大,就得保证 a 1 , a 2 , a 3 , . . . . . a_1,a_2,a_3,..... a1,a2,a3,.....均为不同的素数或素数的整数次幂。求30000内的素数可以用欧拉筛,筛出素数后再…挺惭愧的,赛场上想到这里就卡住了,到最后都每没出来怎么继续做,又打铁了。赛后一看题解恍然大悟,这就是一个典型的“分组背包”问题啊,因为条件“ a 1 , a 2 , a 3 , . . . . . a_1,a_2,a_3,..... a1,a2,a3,.....均为不同的素数或素数的整数次幂”,所以每个素数和它的整数次幂就相当于一组背包。把 30000 30000 30000以内的所有素数都各自为一组,然后一组组的DP就行。

求结果的时候需要利用对数运算法则将 l n ( a 1 ∗ a 2 ∗ a 3 ∗ . . . . ) ln(a_1 * a_2 * a_3 * ....) ln(a1a2a3....)转化为 l n ( a 1 ) + l n ( a 2 ) + l n ( a 3 ) . . . . ln(a_1)+ln(a_2)+ln(a_3).... ln(a1)+ln(a2)+ln(a3)....

简单算一下 x x x以内的素数约为 x / l n ( x ) x/ln(x) x/ln(x)个,30000以内约有3000个素数,极限情况下总执行代码数约 1.5 ∗ 1 0 8 1.5*10^8 1.5108

可能是我算法太慢,这计算量还挺极限的,得提前求出来每个数的log值,否则DP的时候现求就TLE了。最后运行时间300+ms,勉强能接受吧。下面是照着大佬优化后的版本,耗时又再减了一半。

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn=30000+5;
bool number[maxn+5];
int prime[maxn+5];
void euler_sieve(){
	int i,j,cnt=0;
	memset(number,true,sizeof(number));
	memset(prime,0,sizeof(prime));
	
	for(int i=2;i<=maxn;i++){
		if(number[i]) prime[cnt++]=i;
		for(j=0;j<cnt&&prime[j]*i<=maxn;j++){
			number[prime[j]*i]=false;
			if(i%prime[j]==0) break; 
 		}
	}
}

double dp[maxn];
double t_log[maxn];

void dp_run(){
	for(int i=0;prime[i]!=0;i++){
		for(int j=maxn-1;j>=prime[i];j--){
			for(int k=prime[i];k<=j;k*=prime[i]){
				dp[j]=max(dp[j],dp[j-k]+t_log[k]);
			}
		}
	}
}

void init(){
	for(int i=0;i<maxn;i++){
		dp[i]=0.0;
		t_log[i]=log(i);
	}
	euler_sieve();
}

int main(){
	init();
	dp_run();
	int T;
	scanf("%d",&T);
	while(T--){
		int x;
		scanf("%d",&x);
		printf("%.9lf\n",dp[x]);
	}
	return 0;
}

参考资料:

【动态规划】分组背包_yu121380的博客-CSDN博客
2020CCPC(威海) - Clock Master(数论+分组背包)_Falcon的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值