Blue Bridge Cup 2018 Nations Cup Group B 04-- tone watches


Title: tone Watch

Xiao Ming bought a high-end atmosphere on the grade of electronic watches, he is preparing to transfer time yet.
M78 in different units of measurement nebula, time and earth, the n-nebula minutes to one hour M78.
As we all know, there is only one button to watch the current number plus one.
In tune minutes to go, if the currently displayed number is 0, then click the button turns 1, press again becomes 2.
If the current number is n - 1, after press will become zero. As OCD, Xiao Ming must take the time to watch the tone right.
If the time on your watch more than 1 than the current time, the Yaoan n - 1 plus one button can be transferred back to the correct time.
Xiao Ming think, if the watch can add a button to indicate the current number plus k nice ah ......
he wondered if this had + k button and follow the optimal strategy button , transferred to another from any of a number of minutes any of a number of minutes shall be according to the most times .
Note that, when the press button + k, the k numbers if added over n-1, will be on the modulo n.
For example, n = 10, k = 6, when assumed that the current time is 0, double-click button 2 + k is adjusted to 2.

"Input format"
line of two integers n, k, meaning such as the title.

"Output Format"
line of an integer
, said: according to optimal strategy button, transferred from one time to another time shall be according to the most times.

"Sample input"
53

"Sample Output"
2

"Sample explained,"
If the time is correct press 0 times. Otherwise, the relationship between the number and the series of operations Yaoan follows:
1: +1
2: +1, +1
3: +3
4: +3, +1

"Data range"
of 30% of the data 0 <K <n-<=. 5
0 <K <n-<= 100 for the 60% of data
100% of data 0 <k <n <= 100000

Resources for:
peak memory consumption (including virtual machines) <256M
the CPU consumption <1000ms

Please strictly follow the requirements of output, not superfluous to print something like: "Please enter ..." unwanted content.

Note:
main function needs to return 0;
use only ANSI C / ANSI C ++ standard;
do not call the special function depends on the build environment or operating system.
All dependent functions must explicitly in the source file #include <xxx>
can not be provided by engineering common header is omitted.

When the submission process, pay attention to select the desired language and compiler type type.

 

Ideas: breadth-first search, the search value calculated deepest depth of all time difference, the answer was

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int n,k,a[100002];  //a[i]存放当 相差步数为 i 时,需要按的次数 
void bfs(){
	queue<int>q;
	fill(a,a+100002,-1);
	a[0]=0;
	q.push(0);
	int top,x,y;  //x存放按 k增长的所能达到的时刻,y存放按 1增长所能达到的时刻 
	while(!q.empty()) {
		top=q.front();q.pop();
        x=(top+k)%n;
        if(a[x]<0){
        	a[x]=a[top]+1;
        	q.push(x);
		}
		y=top+1;
		if(y!=n){   //相差时刻最多为 n-1 
			if(a[y]<0){
				a[y]=a[top]+1;
				q.push(y);
			}
		}
	}
	cout<<a[top]<<endl;   //队列中最后一个出队的会记录在top最后更新的值 
}
int main(){
	cin>>n>>k;
	bfs();
	return 0;
}


 

Published 42 original articles · won praise 16 · views 3417

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/90440987