博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remainder
阅读量:5305 次
发布时间:2019-06-14

本文共 2324 字,大约阅读时间需要 7 分钟。

Remainder

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2133    Accepted Submission(s): 453

 

Problem Description

Coco is a clever boy, who is good at mathematics. However, he is puzzled by a difficult mathematics problem. The problem is: Given three integers N, K and M, N may adds (‘+’) M, subtract (‘-‘) M, multiples (‘*’) M or modulus (‘%’) M (The definition of ‘%’ is given below), and the result will be restored in N. Continue the process above, can you make a situation that “[(the initial value of N) + 1] % K” is equal to “(the current value of N) % K”? If you can, find the minimum steps and what you should do in each step. Please help poor Coco to solve this problem.

You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.

 

Input

There are multiple cases. Each case contains three integers N, K and M (-1000 <= N <= 1000, 1 < K <= 1000, 0 < M <= 1000) in a single line.

The input is terminated with three 0s. This test case is not to be processed.

 

Output

For each case, if there is no solution, just print 0. Otherwise, on the first line of the output print the minimum number of steps to make “[(the initial value of N) + 1] % K” is equal to “(the final value of N) % K”. The second line print the operations to do in each step, which consist of ‘+’, ‘-‘, ‘*’ and ‘%’. If there are more than one solution, print the minimum one. (Here we define ‘+’ < ‘-‘ < ‘*’ < ‘%’. And if A = a1a2...ak and B = b1b2...bk are both solutions, we say A < B, if and only if there exists a P such that for i = 1, ..., P-1, ai = bi, and for i = P, ai < bi)
 

Sample Input

2 2 2
-1 12 10
0 0 0
 

Sample Output

0
2
*+
 

Author

Wang Yijie
 

Recommend

Eddy

#include
#include
#include
#include
using namespace std;struct node{ int num,step; string oper;};int N,K,M,KM;bool v[1025000];void bfs(){ queue
q; while (!q.empty()) q.pop(); int state=((N+1)%K+K)%K; memset(v,0,sizeof(v)); v[((N%K)+K)%K]=1; node x,tmp; x.num=N; x.step=0; x.oper=""; q.push(x); while (!q.empty()) { x=q.front(); q.pop(); if ((x.num%K+K)%K==state) { printf("%d\n",x.step); //printf("%s\n",x.oper); cout<
<

 

转载于:https://www.cnblogs.com/dramstadt/p/3220646.html

你可能感兴趣的文章
2017.11.12 web中JDBC 方式访问数据库的技术
查看>>
Pat1128:N Queens Puzzle
查看>>
占位 SC
查看>>
《算法心得:高效算法的奥秘(原书第2版)》
查看>>
知道力——彻底超越执行力的25条职场新思维
查看>>
序号的结构层次顺序
查看>>
AC自动机
查看>>
LCA算法解析-Tarjan&倍增&RMQ
查看>>
BZOJ4241 历史研究 莫队 堆
查看>>
BZOJ1191 [HNOI2006]超级英雄Hero 二分图匹配
查看>>
进制转换2016/3/5
查看>>
片段缓存
查看>>
【转】浅谈JavaScript中forEach与each
查看>>
linux指定目录安装软件后,程序找不到共享库问题
查看>>
php连接、操作mysql数据库
查看>>
hello world!
查看>>
hdu1246
查看>>
android开发中如何结束所有的activity
查看>>
面向对象的一个例子
查看>>
公交线路查询接口文档
查看>>