前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces 346C Number Transformation II 构造

Codeforces 346C Number Transformation II 构造

作者头像
全栈程序员站长
发布2022-07-11 09:52:36
900
发布2022-07-11 09:52:36
举报

大家好,又见面了,我是全栈君。

题目链接:点击打开链接

= = 990+ms卡过

代码语言:javascript
复制
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define N 100010
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define ll int
ll n,m,k,a,b;
ll x[N];
bool cmp(ll z,ll y){return z>y;}
set<int>myset;
set<int>::iterator p;
int main(){
	ll i,j;
	while(cin>>n){
		myset.clear();
		for(i=1;i<=n;i++)scanf("%d",&j),myset.insert(j);
		cin>>a>>b;
		if(a==b){puts("0");continue;}
		n = myset.size();
		i = 1;
		for(p = myset.begin(); p!=myset.end(); p++,i++)x[i] = *p;
		sort(x+1,x+1+n,cmp);
		ll step = 0;
		ll l = 1;
		while(x[l]>a && l<=n)l++;
		while(x[l]>(a>>1) && (a-x[l])>(a-b))l++;
		if(l>n){cout<<a-b<<endl;continue;}

		while(a!=b){
			step++;
			ll now = 1, cha = a-b;

			for(i = l;i<=n;i++)
			{
				if(now>=x[i])break;
				ll tmp = a-((a/x[i])*x[i]);
				if(tmp<=now || tmp>cha)continue;
				now = tmp;
			}
			a-=now;
			while(x[l]>a&&l<=n)l++;
			while(x[l]>(a>>1) && (a-x[l])>cha)l++;
			if(l>n)break;
		}
		cout<<step+a-b<<endl;
	}
	return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115207.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档