codeforces.com/contest/251/problem/C
2 seconds
256 megabytes
standard input
standard output
Little Petya likes positive integers a lot. Recently his mom has presented him a positive integer a. There's only one thing Petya likes more than numbers: playing with little Masha. It turned out that Masha already has a positive integer b. Petya decided to turn his number a into the number b consecutively performing the operations of the following two types:
- Subtract 1 from his number.
- Choose any integer x from 2 to k, inclusive. Then subtract number (a mod x) from his number a. Operation a mod x means taking the remainder from division of number a by number x.
Petya performs one operation per second. Each time he chooses an operation to perform during the current move, no matter what kind of operations he has performed by that moment. In particular, this implies that he can perform the same operation any number of times in a row.
Now he wonders in what minimum number of seconds he could transform his number a into number b. Please note that numbers x in the operations of the second type are selected anew each time, independently of each other.
The only line contains three integers a, b (1 ≤ b ≤ a ≤ 1018) and k (2 ≤ k ≤ 15).
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print a single integer — the required minimum number of seconds needed to transform number a into number b.
10 1 4
6
6 3 10
2
1000000000000000000 1 3
666666666666666667
In the first sample the sequence of numbers that Petya gets as he tries to obtain number b is as follows: 10 → 8 → 6 → 4 → 3 → 2 → 1.
In the second sample one of the possible sequences is as follows: 6 → 4 → 3.
题意:
给出 a b k
求a变到b的最小步数,每一步可以选择两种变化中的一种:a=a-1和a=a-a%i (2<=i<=k);
代码:
//k最大只有15,首先要想到2~15的lcm必然是一个循环节,然后就可以利用分块的思想,分成(a-1)/lcm+1块,每一块
//的大小是lcm,然后中间整个块的可以计算(每一块最大不会超过360360),两边的剩余的也可以计算了。这里我用的
//记忆化bfs找的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=;
int k,vis[MAXN];
int gcd_(int x,int y) { return y==?x:gcd_(y,x%y); }
int lcm_(int x,int y) { return x/gcd_(x,y)*y; }
ll bfs(ll x,ll y)
{
queue<ll>q;
memset(vis,-,sizeof(vis));
q.push(x);
vis[x-y]=;
while(!q.empty()){
ll now=q.front();q.pop();
if(now==y) return vis[];
for(int i=;i<=k;i++){
ll xx=now-now%i;
if(xx<y||vis[xx-y]!=-) continue;
vis[xx-y]=vis[now-y]+;
q.push(xx);
}
ll xx=now-;
if(vis[xx-y]!=-) continue;
vis[xx-y]=vis[now-y]+;
q.push(xx);
}
}
int main()
{
ll a,b,ans=;
cin>>a>>b>>k;
if(k==){
cout<<a-b<<"\n";
return ;
}
ll last=;
for(int i=;i<=k;i++) last=lcm_(last,i);
ll tmp1=a/last;
ll tmp2=(b-)/last+;
if(tmp1>tmp2) ans=bfs(a,tmp1*last)+(tmp1-tmp2)*bfs(last,)+bfs(last*tmp2,b);
else ans=bfs(a,b);
cout<<ans<<"\n";
return ;
}
codeforces.com/contest/251/problem/C的更多相关文章
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- Xcode中的Target
Xcode中的Target,主要包含下面几点知识: Target依赖 Build Phase Build Rule Target依赖 Target的依赖关系表示一个Target要构建成功,必先依赖于其 ...
- Dailu Scrum (2015/10/27)
在周日晚上PM已经为大家分配了部分的代码修改工作,今天晚上PM召集了被分配代码工作的3个DEV一起讨论要求修改的代码.在共同讨论的过程中确有发现以下代码的不规范之处,PM当即要求我们先要修改规范代码的 ...
- 20172319 实验二《Java面向对象程序设计》实验报告
20172319 2018.04.17-30 实验二<Java面向对象程序设计>实验报告 课程名称:<程序设计与数据结构> 学生班级:1723班 学生姓名:唐才铭 学生学号:2 ...
- Sprint会议3
昨天:熟悉了一下软件操作,设计了图标. 今天:今天满课,没有做什么实质性的进展. 遇到问题:由于没干什么,也没遇到什么问题.
- Chapter 6 面向对象基础
面向对象=对象+类+继承+通信,如果一个软件系统采用这些概念来建立模型并给予实现,那么它就是面向对象的.面向对象的软件工程方法是面向对象方法在软件工程领域的全面运用涉及到从面向对象分析.面向对象设计. ...
- 可视化实时Web日志分析工具-goaccess
说到web服务器就不得不说Nginx,目前已成为企业建站的首选.但由于种种历史原因,Nginx日志分析工具相较于传统的apache.lighthttp等还是少很多. 今天就和大家分享一个非常强大的实时 ...
- fcn模型训练及测试
1.模型下载 1)下载新版caffe: https://github.com/BVLC/caffe 2)下载fcn代码: https://github.com/shelhamer/fcn.berkel ...
- 剑指offer:用两个栈实现队列
题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 可以用stack1来存所有入队的数.在出队操作中,首先将stack1中的元素清空,转移到sta ...
- Effective Modern C++翻译(5)-条款4:了解如何观察推导出的类型
条款4:了解如何观察推导出的类型 那些想要知道编译器推导出的类型的人通常分为两种,第一种是实用主义者,他们的动力通常来自于软件产生的问题(例如他们还在调试解决中),他们利用编译器进行寻找,并相信这个能 ...
- Python2X和Python3X的区别
python2X:源码重复不规范python3X:整合源码,更清晰简单优美. python2X:默认的编码是ascii (解决办法为第一行添加 : #-*- encoding:ut ...