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. HDU 4925 Apple Tree(模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种 ...

  2. [OpenJudge 3061]Flip The Card

    [OpenJudge 3061]Flip The Card 试题描述 There are N× Ncards, which form an N× Nmatrix. The cards can be p ...

  3. ubuntu 快速安装jre

    sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-i ...

  4. nc 常用命令

    nc(NetCat),在网络工具中有”瑞士军刀”的美誉,它短小精悍,功能强大,下面分享一些我平时经常用到的功能,更多的功能请google之. 1.基本参数想要连接到某处: nc [-options] ...

  5. Django中如何查找模板

    参考:http://my.oschina.net/zuoan001/blog/188782 Django的setting中有关找模板的配置有如下两个: TEMPLATE_LOADERS TEMPLAT ...

  6. xml 解析 java 基础复习

    document  解析 sax  解析 dom4j 解析(摘自csdn redarmychen) dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常 ...

  7. VC++遇到的错误汇集

    1.在程序头添加#include "stdafx.h" 和#include <afx.h>时会出现以下错误 在Win32Project下使用,出现“error C118 ...

  8. codeforces A. IQ Test 解题报告

    题目链接:http://codeforces.com/problemset/problem/328/A 一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also prin ...

  9. jsp url传值乱码

    <Connector port="8080" maxHttpHeaderSize="8192" minProcessors="10"  ...

  10. Ajax案例(使用ajax进行加法运算)

    此案例功能实现了一边看视频一边进行加法运算,而加法运算时页面不会刷新请求 ajax代码: <script type="text/javascript" src="j ...