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. python实现京东秒杀

    # _*_coding:utf-8_*_ from selenium import webdriver import datetime import time driver = webdriver.C ...

  2. HTML5文件操作API

    HTML5文件操作API       一.文件操作API 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或 ...

  3. 使用HttpClient4.5实现HTTPS的双向认证

    说明:本文主要是在平时接口对接开发中遇到的为保证传输安全的情况特要求使用https进行交互的情况下,使用httpClient4.5版本对HTTPS的双向验证的  功能的实现    首先,老生常谈,文章 ...

  4. 前端之bootstrap模态框

    简介:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. Modal简介 Modal实现弹出表单 M ...

  5. 利用java反射读写csv中的数据

      前一段有个需求需要将从数据库读取到的信息保存到csv文件中,在实现该需求的时候发现资料比较少,经过收集反射和csv相关资料,最终得到了如下程序.  1.在使用java反射读取csv文件数据时,先通 ...

  6. Linux--初次体验

    关于Linux已经听闻很久的大名了,但是一直没有机会来使用,这次趁着放假的机会,来体验一把Linux吧. 之前使用visuabox和Ubuntu16,但是虚拟机总是不能连接互联网,在虚拟机上面无法上网 ...

  7. C#-获取字符的GBK编码值

    public static int GetGBKValue(string key) { byte[] gbk = Encoding.GetEncoding("GBK").GetBy ...

  8. apigw鉴权分析(1-5)亚马逊 - 鉴权分析

    一.访问入口 https://developer.amazon.com/public/zh 二.鉴权方式分析 三.分解结论

  9. MySql查询正在进行中的事务

    用法 SELECT * FROM information_schema.INNODB_TRX 这个只能查询此刻正在进行中的事务,已经完成的是查不到的 表字段定义 The INFORMATION_SCH ...

  10. CodeForces 1B-字符串,进制转换与数学

    一个萌新的成长之路 Background 同学们都回家了,只有我和wjh还有邢神在机房敲代码,吃random口味的方便面-- Description Translated by @PC_DOS fro ...