跳到正文

题解

题解 P3303 【[SDOI2013]淘金】

gyh20约 5 分钟
原文
文章信息与版本

文章信息

发布
2020年3月22日
收录
2026年6月14日
更新
2026年11月18日
篇幅
约 5 分钟
获赞
4
题目
P3303

版本

  1. v1当前

数位 DP + 贪心。

求最大的 f(i,j)f(i,j) 不好算,因为这是二维的,我们把它转为一个一维的问题。

首先,因为每一位上的数都不大于 99 (显然),所以乘积肯定是 2a1×3a2×5a3×7a42^{a1}\times 3^{a2}\times 5^{a3}\times 7^{a4},而这样的数一定不多。

可以预处理出有哪些数是符合上述条件的,然后计算出有多少个将每一位分解后由 a1a1 个 22,a2a2 个 33,a3a3 个 55,a4a4 个 77 组成的且不大于 nn 的数(预处理出 0∼90\sim 9 的质因数分解情况)。这是一个很简单的数位 DP。

将一维得到的结果排序,用优先队列维护当前最大值。

C++
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<map>
#define int long long
#define re register
#define M 1000000007
using namespace std;
inline int read(){
	re int t=0;
	re char v=getchar();
	while(v<'0')v=getchar();
	while(v>='0'){
		t=(t<<3)+(t<<1)+v-48;
		v=getchar();
	}
	return t;
}
int h[200002],tot,n,f[14][45][28][20][16][2],cnt[10][4]={{0,0,0,0},{0,0,0,0},{1,0,0,0},{0,1,0,0},{2,0,0,0},{0,0,1,0},{1,1,0,0},{0,0,0,1},{3,0,0,0},{0,2,0,0}},p[17],ans[200002],k,m,num;
map<int,int>mp;
inline void dfs(re int x){
	if(mp[x])return;
	mp[x]=1;
	if(x>n)return;
	h[++tot]=x;
	dfs(x<<1),dfs((x<<1)+x),dfs((x<<2)+x),dfs((x<<2)+(x<<1)+x);
}
inline int dp(re int x,re int a,re int b,re int c,re int d,re int z,re bool flag){

	if(a<0||b<0||c<0||d<0)return 0;
	if(x==0)return !a&&!b&&!c&&!d&&!z;//	printf("%lld %lld %lld %lld %lld %lld %d\n",x,a,b,c,d,z,flag);
	if(~f[x][a][b][c][d][z]&&!flag)return f[x][a][b][c][d][z];
	re int ret=0;
	for(re int i=!z;i<=(flag?p[x]:9);++i){
		
	ret+=dp(x-1,a-cnt[i][0],b-cnt[i][1],c-cnt[i][2],d-cnt[i][3],z&&(!i),flag&&(i==p[x]));//printf("%lld\n",f[x][a][b][c][d][z][flag]);
	}
	if(!flag)f[x][a][b][c][d][z]=ret;
	return ret;
}
struct node{
	int x,y;
	const bool operator <(const node xx)const{
	return ans[xx.x]*ans[xx.y]>ans[x]*ans[y];
}
};
inline int calc(re int x){
	re int a=0,b=0,c=0,d=0,y=x;
	while(!(x&1))x>>=1,++a;
	x=y;
	while(!(x%3))x/=3,++b;
	x=y;
	while(!(x%5))x/=5,++c;
	x=y;
	while(!(x%7))x/=7,++d;
	return dp(p[0],a,b,c,d,1,1);
}
priority_queue<node>q;
signed main(){
	re int xx=n=read();k=read();
	p[0]=0;
	while(xx){
		p[++p[0]]=xx%10;
		xx/=10;
	}
	dfs(1);
	memset(f,-1,sizeof(f));
	for(re int i=1;i<=tot;++i){
	ans[i]=calc(h[i]);
	//printf("%lld %lld\n",h[i],ans[i]);
	}
	sort(ans+1,ans+tot+1);
	for(re int i=1;i<=tot;++i)q.push(node{i,tot});
	for(re int i=1;i<=k;++i){
		re int x=q.top().x,y=q.top().y;
		q.pop();
	//	printf("%lld %lld\n",x,y);
		m+=ans[x]%M*ans[y]%M;
		m%=M;
		if(y>1)q.push(node{x,y-1}); 
	}
	printf("%lld",m);
}
原文
存档评论2 条抓取于