C. Number Transformation II
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence of positive integers x1, x2, ..., xn and two non-negative integers a and b. Your task is to transform a into b. To do that, you can perform the following moves:

  • subtract 1 from the current a;
  • subtract a mod xi (1 ≤ i ≤ n) from the current a.

Operation a mod xi means taking the remainder after division of number a by number xi.

Now you want to know the minimum number of moves needed to transform a into b.

Input

The first line contains a single integer n (1 ≤  n ≤ 105). The second line contains n space-separated integers x1, x2, ..., xn(2 ≤  xi ≤ 109). The third line contains two integers a and b (0  ≤ b ≤  a ≤ 109, a - b ≤ 106).

Output

Print a single integer — the required minimum number of moves needed to transform number a into number b.

 
Examples
input
3
3 4 5
30 17
output
6
input
3
5 6 7
1000 200
output
206
/*
CodeForces346 C. Number Transformation II 给你两个数a,b
1.每次对于当前的a减去1
2.每次对于当前的a减去 a%(ta[i]) 求最少多少次能得到b
类似于贪心,每次减去尽可能多的值。但是一直TLE- -. 后来可以发现a~a-a%ta[i]的所有值
如果减去a%ta[i],都会等于同一个值。 如果当a-a%ta[i] < b时,ta[i]可以说在后面的
搜索就没有作用了.于是乎把ta[i]除去. hhh-2016-04-16 17:15:20
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <math.h>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int mod = 1000000009;
const int maxn = 100040;
int ta[maxn];
int main()
{
int n;
while( scanf("%d",&n) != EOF)
{
int num = 0;
int a,b;
for(int i =0; i < n; i++)
{
scanf("%d",&ta[i]);
}
sort(ta,ta+n);
n = unique(ta,ta+n)-ta;
scanf("%d%d",&a,&b);
while(a > b)
{
int cur = a - 1;
for(int i =n-1; i >= 0; i--)
{
if(a-a%ta[i] >= b)
cur = min(a-a%ta[i],cur);
}
a = cur;
num ++;
if(a == b) break;
while(n)
{
if(a-a%ta[n-1] < b)
n--;
else
break;
}
}
printf("%d\n",num);
}
return 0;
}

  

CodeForces346 C. 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. 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转 ...

  3. Codeforces 346C Number Transformation II 构造

    题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...

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

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

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

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

  6. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  7. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  8. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  9. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. 20145237 实验二 “Java面向对象程序设计”

    20145237 实验二 “Java面向对象程序设计” 实验内容 • 理解并掌握面向对象三要素:封装.继承.多态 • 初步掌握UML建模 • 熟悉S.O.L.I.D原则 • 使用TDD设计实现复数类 ...

  2. (function(root,factory){})(this,function($){}) 一个立即执行的匿名函数自调

    因为新公司用到ocx 我就开始看原来的代码 无意中发现这个 可能原来比较low吗(虽然现在也很low吧)没发现这个东东 还可以这样写 于是乎我开始了探索 完整代码如下 HTML <div id= ...

  3. Java中Math类的常用方法

    public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...

  4. js中严格模式

    我们在js中可以使用"use strict";定义了我们在接下来的文档输写中 将按照严格模式进行: function(){ "use strict'; ;// 在这里我们 ...

  5. redis命令详解

      redis中添加key value元素:set key value;       获取元素:get key ;   redis中添加集合:lpush key value1 value2 value ...

  6. 小tip: 使用CSS将图片转换成黑白(灰色、置灰)

    可能早就知道,像汶川这种糟糕的日子网站全灰在IE下是可以轻松实现的(filter: gray;),不过,当时,其他浏览器是无解的.不过,时代发展,如今,CSS3的逐步推进,我们也开始看到“黑白效果”大 ...

  7. JavaScript 重点笔记

    JavaScript 重点笔记 ## 数组 // 必须掌握 - arr.length:获取数组元素的长度 - arr.splice(起始位置,长度):从数组中添加或删除元素. - arr.indexO ...

  8. CWMP开源代码研究——stun的NAT穿透

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 参考: http://www.cnblogs.com/myblesh/p/6259765.htm ...

  9. Java Servlet(十一):一个servlet被10个浏览器客户端访问时会创建几个servlet实例?

    一般Servlet只初始化一次(只有一个实例).对于更多的客户端请求,Server创建新的请求和响应对象,仍然激活此Servlet的service()方法,将这两个对象作为参数传递给该方法.如此重复以 ...

  10. SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...