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的更多相关文章

  1. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

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

  2. hdu 1788 Chinese remainder theorem again(最小公倍数)

    Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...

  3. HDU 1104 Remainder( BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. Chinese remainder theorem again(中国剩余定理)

    C - Chinese remainder theorem again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:% ...

  5. DHU 1788 Chinese remainder theorem again 中国剩余定理

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. 【Atcoder】AGC022 C - Remainder Game 搜索

    [题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...

  7. HDU 1104 Remainder (BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)

    (多项式的)因式分解定理(factor theorem)是多项式剩余定理的特殊情况,也就是余项为 0 的情形. 0. 多项式长除法(Polynomial long division) Polynomi ...

  9. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

随机推荐

  1. [Effective JavaScript 笔记]第17条:间接调用eval函数优于直接调用

    eval函数不仅仅是一个函数.大多数函数只访问定义它们所在的作用域,而不能访问除此之外的作用域(词法作用域).eval函数具有访问调用它时的整个作用域的能力.编译器编写者首次设法优化js时,eval函 ...

  2. cocos2d回忆

    凭借自己的回忆想想看自己都学到了那些知识,这是小学的时候当初中老师的外公给我说的,现在想想,CCDirector,CCNode,CCScene,CCSprite这几个类,然后是坐标,锚点,CCNode ...

  3. 多层神经网络与C++实现

    BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...

  4. 手把手教你用Python爬虫煎蛋妹纸海量图片

    我们的目标是用爬虫来干一件略污事情 最近听说煎蛋上有好多可爱的妹子,而且爬虫从妹子图抓起练手最好,毕竟动力大嘛.而且现在网络上的妹子很黄很暴力,一下接受太多容易营养不量,但是本着有人身体就比较好的套路 ...

  5. 双参数Bellman-ford带队列优化类似于背包问题的递推

    为方便起见,将Bellman-ford队列优化称为SPFA,= = 抓住 ZMF (ZMF.pas/c/cpp) 题目描述 话说这又是一个伸手不见五指的夜晚,为了机房的电子竞技事业永远孜孜不倦的 ZM ...

  6. 【Django】Django 文件下载最佳实践

    代码: from django.http import StreamingHttpResponse def big_file_download(request): # do something... ...

  7. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. object-c 继承多态 动态数据类型

    在c#中我们知道有继承的.同样在object-c中也有继承. 例如我们写一个人类(父),一个学生类.我们可以这么写: demo: @interface Person:NSobject{ NSStrin ...

  9. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

  10. sybaseIQ索引类型和使用注意事项

    1. FP(Fast Projection)此索引为默认的索引形式,在创建表时系统自动设置此索引. 特点:用于SELECT.LIKE '%sys%'.SUM(A+B).JOIN操作等语句. 此类型索引 ...