Remainder
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<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int num,step;
string oper;
};
int N,K,M,KM;
bool v[1025000];
void bfs()
{
queue<node> 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<<x.oper<<endl;
return;
}
tmp=x;
tmp.step++;
tmp.num=(x.num+M)%KM;
tmp.oper+="+";
if (!v[(tmp.num%K+K)%K])
{
q.push(tmp);
v[(tmp.num%K+K)%K]=1;
}
tmp=x;
tmp.step++;
tmp.num=(x.num-M)%KM;
tmp.oper+="-";
if (!v[(tmp.num%K+K)%K])
{
q.push(tmp);
v[(tmp.num%K+K)%K]=1;
}
tmp=x;
tmp.step++;
tmp.num=(x.num*M)%KM;
tmp.oper+="*";
if (!v[(tmp.num%K+K)%K])
{
q.push(tmp);
v[(tmp.num%K+K)%K]=1;
}
tmp=x;
tmp.step++;
tmp.num=(x.num%M)%KM;
tmp.oper+="%";
if (!v[(tmp.num%K+K)%K])
{
q.push(tmp);
v[(tmp.num%K+K)%K]=1;
}
}
printf("0\n");
}
int main()
{
while (scanf("%d%d%d",&N,&K,&M)!=EOF)
{
if (N+K+M==0) return 0;
KM=K*M;
bfs();
}
return 0;
}
Remainder的更多相关文章
- hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdu 1788 Chinese remainder theorem again(最小公倍数)
Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...
- HDU 1104 Remainder( BFS(广度优先搜索))
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Chinese remainder theorem again(中国剩余定理)
C - Chinese remainder theorem again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:% ...
- DHU 1788 Chinese remainder theorem again 中国剩余定理
Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 【Atcoder】AGC022 C - Remainder Game 搜索
[题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...
- HDU 1104 Remainder (BFS(广度优先搜索))
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)
(多项式的)因式分解定理(factor theorem)是多项式剩余定理的特殊情况,也就是余项为 0 的情形. 0. 多项式长除法(Polynomial long division) Polynomi ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
随机推荐
- convert jar to java
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 首先解压一下下载的jar文件,extract jar file , $jar -xvf file.j ...
- Opencv Cookbook阅读笔记(四):用直方图统计像素
灰度直方图的定义 灰度直方图是灰度级的函数,描述图像中该灰度级的像素个数(或该灰度级像素出现的频率):其横坐标是灰度级,纵坐标表示图像中该灰度级出现的个数(频率). #include <open ...
- tomcat配置文件之Server.xml
Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...
- BNUOJ 1037 精神控制
XsuagrX喜欢到处唬人,各种唬.这不,经过刻苦修炼,他终于掌握了Bane Element的Ultra绝技加强版,恶魔掌控(快捷键F)(YY中&……).当XsugarX对某个人胡言乱语Q@# ...
- GraphicsMagick为图片添加水印
GraphicsMagick号称图像处理领域的瑞士军刀.提供了健壮及高效的图像处理工具包和库,支持超过88种主流图片格式包括:BMP,GIF,JPEG,JPEG-2000,PNG,PDF,PNM,TI ...
- Android-自定义meta-data扩展数据
在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义: [html] view plaincopy <!-- appid --> < ...
- 【持续集成】使用Jenkins实现多平台并行集成
使用Jenkins实现多平台并行集成 二月 15, 2012 暂无评论 我们的后端C应用都是支持跨平台的,至少目前在Linux和Solaris上运行是没有问题的,这样一来我们在配置持续集成环境时就要考 ...
- KDD-CUP Proposal
From 鞠源 已有 1303 次阅读 2012-11-25 21:09 |系统分类:科研笔记|关键词:会议 领域 justify 知识 KDDCUP - Competition is a stron ...
- iOS block 声明时和定义时的不同格式
今天写程序时,在实现一个block时总提示格式错误,对比api的block参数格式,没发现错误.后来查阅了资料,发现这两个格式是不同的! 具体格式见下方 NSString * (^testBlock) ...
- 【2016-08-06】QTableWidget的一些用法总结
1. QTableWidget的列宽如何自适应显示区域大小? QTableWidget的列头继承自QHeaderView,因此如果不使用固定列宽而需要Table中多列的列宽自适应显示区域大小的话, 可 ...