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. 软件工程第三次作业-结对作业NO.1

    第一次结对作业 结对人员: 潘伟靖 170320077 张 松 170320079 方案分析 我们对所供的资料进行分析,如下: 从提供的资料可以看出,需要解决的问题以及满足的需求主要有两类目标用户,各 ...

  2. servlet线程同步问题-代码实现同步(转)

    从servlet的生命周期中,我们知道,当第一次访问某个servlet后,该servlet的实例就会常驻 内存,以后再次访问该servlet就会访问同一个servlet实例,这样就带来多个用户去访问一 ...

  3. 根据抽象工厂实现的DBHelpers类

    public abstract class DBHelper { public static SqlConnection conn = new SqlConnection("server=l ...

  4. JAVA_SE基础——22.面向对象的概念

    我写博客是为了提升自己和为了进入黑马程序员学习,还有分享些自己的心得给大家,希望能帮助大家学习JAVA. 我是自学的,如果写的有错误或者能更好的修改的请提出. 在这里我先引用下<think in ...

  5. dede观看总结自己总结

    知识点一:{dede:arclist channelid="18" addfields="language,pfz" limit="0,5" ...

  6. C#之Socket通信

    0.虽然之前在项目中也有用过Socket,但始终不是自己搭建的,所以对Server,Clinet端以及心跳,断线重连总没有很深入的理解,现在自己搭建了一遍加深一下理解. 服务端使用WPF界面,客户端使 ...

  7. JVM学习记录

    本博客是为了自己学习JVM而建立,只记录一些自己学习的经过. 最近在看<深入理解Java虚拟机>这本书,里面的内容,很是乏味,因为看不懂所以就会觉得很枯燥,觉得很枯燥看着看着就犯困,然后就 ...

  8. Angular 学习笔记 ( CDK - Observers )

    <div class="projected-content-wrapper" (cdkObserveContent)="projectContentChanged( ...

  9. GIT入门笔记(12)- 删除文件、提交删除和恢复删除

    在Git中,删除也是一个修改操作,我们实战一下, 1.先添加add一个新文件test.txt到Git并且提交commit到本地版本库: $ git add test.txt$ git commit - ...

  10. SQL Server 利用触发器对多表视图进行更新

    其步骤就是:利用update操作触发器产生的2个虚拟表[inserted]用来存储修改的数据信息和[deleted]表,然后将对应的数据更新到对应数据表中的字段信息中: 1.首先创建3个表: a.信息 ...