题目链接:点击打开链接

= = 990+ms卡过

#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;
}

Codeforces 346C Number Transformation II 构造的更多相关文章

  1. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

  2. Codeforces 346C Number Transformation II 贪心(复杂度计算)

    题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...

  3. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

  5. cf201.div1 Number Transformation II 【贪心】

    1 题目描述: 被给一系列的正整数x1,x2,x3...xn和两个非负整数a和b,通过下面两步操作将a转化为b: 1.对当前的a减1. 2.对当前a减去a % xi (i=1,2...n). 计算a转 ...

  6. CSUOJ 1299 - Number Transformation II 打表预处理水DP

    http://122.207.68.93/OnlineJudge/problem.php?id=1299 第二个样例解释.. 3 6 3->4->6..两步.. 由此可以BFS也可以DP. ...

  7. Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜

    题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...

  8. codeforces 1059C. Sequence Transformation【构造】

    题目:戳这里 题意:有1,2,3...n这n个数,求一次这些数的gcd,删去一个数,直到剩下一个数为止.输出这n个gcd的最大字典序. 解题思路:一开始的gcd肯定是1,要让字典序最大,我们可以想到下 ...

  9. 2016级算法第二次上机-F.ModricWang's Number Theory II

    891 ModricWang's Number Theory II 思路 使得序列的最大公约数不为1,就是大于等于2,就是找到一个大于等于2的数,它能够整除序列中的所有数. 考虑使得一个数d整除数组中 ...

随机推荐

  1. Robots on a grid(DP+bfs())

    链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...

  2. Django学习(二)

    一  高亮显示 <script type="text/javascript"> $(document).ready(function () { $("#nav ...

  3. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  4. 12 个 Web 设计师必备的 Bootstrap 工具

    转自:http://www.oschina.net/translate/12-best-bootstrap-tools-for-web-designers Bootstrap是一个非常棒的前端网站开发 ...

  5. C#设计模式——工厂方法

    一.为什么需要工厂方法      工厂方法可以这么理解.你规定了一个规范,只要符合这个规范.那么就可以按照你的方式进行操作,这样你就无需知道具体操作的对象是什么,具有什么特性等等,可以进行统一化的操作 ...

  6. C#的泛型委托与闭包函数

    前些天Wendy问我说Func<T, ResultT>是个什么意思,初学C#都觉得这样的写法很奇葩,甚至觉得这样写有点诡异,其实以我来看,这是体现C#函数式编程的又一个亮点. 从MSDN上 ...

  7. C#手动做一个负载均衡服务器

    思路 负载均衡服务器最出名的当数 Nginx了.Nginx服务器通过异步的方式把连接转发给内网和N个服务器,用来分解单台应用服务器的压力,了解了原理及场景后,用C#来实现一个.思路如下: 1. 使用一 ...

  8. tomcat通过conf-Catalina-localhost目录发布项目详解

    Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需 ...

  9. Minifying Angular应用时产生的问题

    一.产生的问题 如果你正在进行AngularJS的项目开发,生产时Minified JS文件有没有遇到下面问题: angular.module("myApp", []) .cont ...

  10. Android 动态获取ListView的高度

    public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = l ...