[堆 思路题] BZOJ 4524 [Cqoi2016]伪光滑数

16 篇文章 0 订阅


可持久化可并堆+DPhttp://blog.csdn.net/liuguangzhe1999/article/details/51132255

我也不想这个有趣的方法就这么绝迹了呢 但是我不会打

堆:http://blog.csdn.net/dropd/article/details/51138254



#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(ll &x){
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

namespace hash_map{
	struct node{
		ll x; int next;
	}G[1000005];
	int head[10000007],P=10000007,inum;
	inline void add(ll x){
		G[++inum].x=x; G[inum].next=head[x%P]; head[x%P]=inum;
	}
	inline int find(ll x){
		for (int p=head[x%P];p;p=G[p].next)
			if (G[p].x==x)
				return p;
		return 0;
	}
}

const ll prime[]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127};
const int pnum=31;

struct data{
	ll num;
	int a[32];
}A[1000005];
int cnt;

ll N,K;
priority_queue<ll> Q;

int main()
{
	ll itmp,ans,pos;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(N); read(K);
	for (int i=1;i<=pnum && prime[i]<=N;i++)
	{
		itmp=prime[i];
		for (int j=1;itmp<=N && itmp>0;j++,itmp*=prime[i])
		{
			hash_map::add(itmp);
			A[++cnt].num=itmp; A[cnt].a[i]=j;
			Q.push(itmp);
		}
	}
	while (K--)
	{
		ans=Q.top(); Q.pop();
		if (!K) break;
		pos=hash_map::find(ans);
		for(int i=2;i<=pnum;i++)
			if(A[pos].a[i])
            {
				itmp=ans/prime[i]*prime[i-1];
				if(!hash_map::find(itmp))
            	{
				   	Q.push(itmp);
					hash_map::add(itmp);
					A[++cnt].num=itmp;
					for(int j=1;j<=pnum;j++)
						A[cnt].a[j]=A[pos].a[j];
                	A[cnt].a[i]--;
					A[cnt].a[i-1]++;
               	}
            }
	}
	printf("%lld\n",ans);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值