Number Transformation CF 251C

7 篇文章 0 订阅
6 篇文章 0 订阅

http://codeforces.com/contest/251/problem/C

关键就是注意到从i*lcm(1,2 ...K)转移到(i-1)*lcm(1, 2...K)的代价等于lcm(1, 2...K)转移到0的代价,

而且由于第一种操作只能减小1而第二种操作不可能越过过i*lcm(1, 2...K)的(可以用反证法证明),

所以转移的过程必然会经过所有的i*lcm(1, 2, ...K)(a <= i*lcm(1, 2...K) <= b),bfs求出转移代价再

乘上区间个数,再处理一下俩个边界就可以了。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(10010);
const int MAXM(10010);
const int MAXE(2100010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-6);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}

int gcd(int a, int b){
	return b? gcd(b, a%b): a;
}
int lcm(int a, int b){
	return a/gcd(a, b)*b;
}
bool vis[400010];
struct NODE{
	int s, c;
	NODE(){}
	NODE(int s_, int c_): s(s_), c(c_){}
} que[400010];
int front, back;
int bfs(LL a, LL b, LL K){
	memset(vis, 0, sizeof(vis[0])*(a+1));
	front = back = 0;
	que[back++] = NODE(a, 0);
	vis[a] = true;
	while(front < back){
		NODE cur = que[front++];
		if(cur.s == b) return cur.c;
		int ts = cur.s-1;
		if(ts >= b && !vis[ts]){
			que[back++] = NODE(ts, cur.c+1);
			vis[ts] = true;
		}
		for(int i = 2; i <= K; ++i){
			ts = cur.s-cur.s%i;
			if(ts >= b && !vis[ts]){
				que[back++] = NODE(ts, cur.c+1);
				vis[ts] = true;
			}
		}
	}
}

int main(){
	LL a, b, K;
	while(~scanf("%I64d%I64d%I64d", &a, &b, &K)){
		int lc = 1;
		for(int i = 2; i <= K; ++i) lc = lcm(lc, i);
		LL step = a/lc-(b/lc+(b%lc? 1: 0)), ans;  //step = floor(a/lc)-ceiling(b/lc)
		if(step < 0) ans = bfs(a%lc, b%lc, K);    
		else ans = bfs(lc, b%lc? b%lc: lc, K)+step*bfs(lc, 0, K)+bfs(a%lc, 0, K);
		printf("%I64d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值