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. 他们在军训,我在搞 OI(四)

    (怎么自动变成两天一更了?) ——因为我菜啊 T_T Day 5 今天上午刷得爽啊!5 道 NOIP,前四题直接 1A,然而最后一题还是 WA 了一发才 A... 第一题是个简单的贪心,题意大概是 n ...

  2. [BZOJ2423][HAOI2010]最长公共子序列

    [BZOJ2423][HAOI2010]最长公共子序列 试题描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x ...

  3. poj2253 最短路 floyd Frogger

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28825   Accepted: 9359 Descript ...

  4. poj3295

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10453   Accepted: 3967 Descri ...

  5. 大数据之ETL设计详解

    ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...

  6. css定位左移动多少距离

    .main .logo { padding-left:82px; } 说明: .main .logo这个标签的路径离边框的距离是82Px

  7. Java中Set集合的使用

    除了List之外,Set集合接口也经常使用,Set接口中存放的元素是无序的并且是不可重复的,因此被称为数据集: Set接口因为是无序的,所以没有提供像List一样的set方法来修改元素,查找,添加.删 ...

  8. 如何破解excel宏的密码

    http://zhidao.baidu.com/question/140107193.html 最近下载了一个excel模板,使用excel宏编的,但实际需要需更改一下,但是他设置了工作表密码保护,谁 ...

  9. 利用FFmpeg生成视频缩略图 2.3.1

    1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...

  10. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...