Codeforces Round #153 (Div. 1) C Number Transformation bfs

//到达2 , 3 ... k的最小公倍数为lcm

//当x到达lcm的倍数时,x只能减一

//又从a到b一定会过lcm的倍数

//我们将a,b以lcm分割

//那么完整的lcm段的最小数是相同的,所以我们只需要计算一个lcm段的最小值乘以完整lcm段的个数

//以及首尾的不全的lcm段的和即为答案

#include<cstdio>

#include<cstring>

#include<iostream>

#include<queue>

using namespace std ;

const int maxn = 1000010 ;

int vis[maxn] ;

struct node

{

__int64 step;

__int64 value ;

};

int gcd(int a , int b)

{

if(a < b)

swap(a,b);

if(b == 0)

return a ;

return gcd(b, a%b) ;

}

int lcm(int a,int b)

{

return a*b/gcd(a,b) ;

}

queue<struct node>que;

__int64 bfs(__int64 n ,__int64 k ,__int64 en)

{

while(que.size())que.pop();

struct node first = {0,n} ;

que.push(first) ;

vis[n] = 1;

memset(vis, 0 ,sizeof(vis)) ;

while(que.size())

{

struct node now = que.front() ;

que.pop() ;

if((__int64)now.value == en)

return now.step;

for(int i = 1;i <= k;i++)

{

__int64 t ;

if(i == 1)t = now.value - 1;

else t = now.value - now.value%i;

if(t<0 || vis[t])continue ;

struct node next = {now.step+1 , t} ;

que.push(next) ;

vis[t] = 1;

}

}

return 0;

}

int main()

{

//freopen("input.txt","r",stdin) ;

__int64 a , b , k;

while(~scanf("%I64d%I64d%I64d" ,&a ,&b ,&k))

{

int sum= 2;

if(k == 2)

{

printf("%I64d\n" ,a - b) ;

continue ;

}

for(int i = 3;i <= k;i++)

sum = lcm(sum , i) ;

__int64 first = (b+(__int64)sum-1)/(__int64)sum;

__int64 last = a/(__int64)sum;

__int64 ans ;

if(last >= first)

{

ans=(last - first)*(bfs((sum-1) , k , 0) + 1);

ans += bfs(sum,k,b%sum);

ans += bfs(a%sum,k,0);

}

else ans = bfs(a%sum, k ,b%sum);

printf("%I64d\n" ,ans) ;

}

}

时间: 2024-08-29 20:14:13

Codeforces Round #153 (Div. 1) C Number Transformation bfs的相关文章

Codeforces Round #209 (Div. 2)——Prime Number

MySQL使用的是插件式存储引擎. 主要包括存储引擎有:MyISAM,Innodb,NDB Cluster,Maria,Falcon,Memory,Archive,Merge,Federated. 其中最为广泛的是MyISAM 和Innodb两种存储引擎,所以接下来对它们做简单介绍. MyISAM 存储引擎简介 MyISAM 存储引擎的表存储在数据库中,每一个表都被存放为三个以表名命名的物理文件. 1.(.frm文件)任何存储引擎都不可缺少的存放表结构定义信息的文件 2.(.MYD文件)存放表数

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 9:14:02 7 File Name :B.cpp 8 *************************************************/ 9 10 #include

Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a descr

Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操作: 1.求出余下数的gcd,然后将gcd加入答案队列: 2.从中任意删除一个数,如果余下数的个数大于0,回到步骤1. 问答案队列字典序最大是什么. 题解: 这明明是递归在做极其方便,我偏偏用for循环来搞,加各种判断... 首先对于1,2,3...n来说,我们每次删去一个数时,肯定首先删去的是奇数

Codeforces Round #266 (Div. 2)C. Number of Ways

传送门 Description You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you nee

Codeforces Round #153 (Div. 1)BPlaying with Permutations

//可以看出操作1和操作2是一对互逆操作 //即是一次操作1和一次操作2执行后,排列不变 //如果一种操作连续做i次能得到s //如果i刚好等于k,puts("YES") //如果不等,看剩下的k-i次操作能不能用一次操作1和一次操作2做完 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn  = 110 ; int p_

Codeforces Round #153 (Div. 1)APoints on Line

#include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn  = 100010 ; int a[maxn] ; int bit(int l , int r ,int num) { while(l <= r) { int mid = (l + r) >> 1; if(a[mid] <= num) l = mid + 1 ; el

Codeforces Round #514 (Div. 2) C. Sequence Transformation 思维构造

题意 给出一个1-n的集合   gcd 集合里面的所有数  得到的 一个 数   然后自己选择删去一个数   要使得到的数 构成的数列 的字典序最大 思路: gcd所有数 那gcd得到的数肯定要小于数组中最小的数  所以 刚开始都是1   所以优先删去1  那就要使gcd所有数经可能快得到 2 如何快速到2 呢 那就是把奇数全部删掉  那剩下得数最小就为2 了  此时为 2 4 6 8 10....  此刻就从2开始删   当n==3时 有 x ,2x,3x  此时 只有 删 x 2 x   3

CodeCraft-19 and Codeforces Round #537 (Div. 2) A - Superhero Transformation

题目描述 We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name ss can transform to another superhero with name tt if ss can be made equal to tt by chan