Remainder

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3036    Accepted Submission(s): 679

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
*+

 #include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<math.h>
int n , k , m , ini , km ;
int en ;
bool vis[] ;
struct node
{
int w ;
int dir , nxt , step ;
}e[];
int l , r ;
/*
bool cmp (const node &a , const node &b)
{
if (a.step < b.step ) return true ;
if (a.step == b.step ) return a.dir < b.dir ;
return false ;
}*/ int calc (int u , int id)
{
if (id == ) return (u + m) % km;
else if (id == ) return (u - m) % km ;
else if (id == ) return (u * m) % km ;
else return (u % m + m) % m % km;
} bool bfs ()
{
// printf ("ini=%d\n" , ini ) ;
node tmp , ans ;
l = , r = ;
vis[ (n % k + k) % k] = ;
e[l].w = n , e[l].dir = - , e[l].nxt = - , e[l].step = ;
while ( l != r) {
// std::sort (e + l , e + r , cmp ) ;
ans = e[l] ;
// printf ("S---%d = %d\n" , ans.w , ans.step ) ;
for (int i = ; i < ; i ++) {
tmp = ans ;
tmp.w = calc (tmp.w , i) ;
if (vis[(tmp.w % k + k) % k]) continue ; vis[ (tmp.w % k + k) % k] = ;
tmp.dir = i ; tmp.nxt = l ; tmp.step ++ ;
e[r ++] = tmp ;
if ( ((tmp.w % k + k) % k ) == ini) {
// printf ("final : %d\n" , tmp.step ) ;
// printf ("answer:%d\n" , tmp.w ) ;
return true ;
}
// printf ("%d = %d\n" , tmp.w , tmp.step ) ;
}
l ++ ;
}
return false ;
} void dfs (int id , int deep)
{
if (e[id].nxt == -) {
printf ("%d\n" , deep ) ;
return ;
}
// printf ("ID=%d , %d \n" , id , e[id].dir ) ;
dfs (e[id].nxt , deep + ) ;
int t = e[id].dir ;
// printf ("t=%d\n" , t ) ;
if (t == ) printf ("+") ;
else if (t == ) printf ("-") ;
else if (t == ) printf ("*") ;
else if (t == ) printf ("%%") ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d%d" , &n , &k , &m )) {
if (n == && k == && m == ) break ;
memset (vis , , sizeof(vis)) ;
ini = ((n+)%k + k) % k ;
/* if (bfs () ) {puts ("yes") ; printf ("l=%d\n" , l ) ; }
else puts ("no") ;*/
km = k * m ;
if (bfs ()) dfs (r - , ) ;
else printf ("") ;
puts ("") ; //puts ("") ;
}
return ;
}

wa到死。
一个个坑等你跳,比如说printf ("%%") ;

% (k * m) ;

mod : a mod b = (a % b + b) % b ;

http://www.cnblogs.com/qiufeihai/archive/2012/08/28/2660272.html

hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))的更多相关文章

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

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

  2. hdu - 1104 Remainder (bfs + 数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...

  3. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  4. HDU 1104 Remainder (BFS)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1104 题意:给你一个n.m.k,有四种操作n+m,n-m,n*m,n%m,问你最少经过多少步,使得最后 ...

  5. HDU 1104 Remainder

    与前一题类似,也是BFS+记录路径, 但是有很多BUG点, 第一MOD操作与%不同i,其实我做的时候注意到了我们可以这样做(N%K+K)%K就可以化为正数,但是有一点要注意 N%K%M!=N%M%K; ...

  6. HDU 1104 Remainder (BFS求最小步数 打印路径)

    题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...

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

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

  8. hdu 1104 数论+bfs

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

  9. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

随机推荐

  1. AngularJs ngInclude、ngTransclude

    这两个都是HTML DOM嵌入指令 ngInclude 读取,编译和插入外部的HTML片段. 格式:ng-include=“value”<ng-include src=”value” onloa ...

  2. Linux命令(31):zip/unzip命令-打包压缩

    zip命令功能说明    zip程序即是文件压缩工具也是文件归档工具,可以对文件或者目录进行压缩或解压,压缩格式为zip.在Linux系统中,gzip才是主要的压缩指令,而bzip2仅次之.Linux ...

  3. 高性能JavaScript笔记二(算法和流程控制、快速响应用户界面、Ajax)

    循环 在javaScript中的四种循环中(for.for-in.while.do-while),只有for-in循环比其它几种明显要慢,另外三种速度区别不大 有一点需要注意的是,javascript ...

  4. POJ1050To the Max(求最大子矩阵)

    题目链接 题意:给出N*N的矩阵,求一个子矩阵使得子矩阵中元素和最大 分析: 必备知识:求一组数的最大连续和 int a[N]; ,maxn = -INF; ; i <= n; i++) { i ...

  5. vimium Keyboard Bindings

    Modifier keys are specified as `<c-x>`, `<m-x>`, and `<a-x>` for ctrl+x, meta+x, a ...

  6. JavaScript中捕获/阻止捕获、冒泡/阻止冒泡

    JavaScript中捕获/阻止捕获.冒泡/阻止冒泡 事件流描述的是从页面中接收事件的顺序.提出事件流概念的正是IE和Netscape,但是前者提出的是我们常用的事件冒泡流,而后者提出的是事件捕获流. ...

  7. 机器学习笔记—Logistic回归

    本文申明:本系列笔记全部为原创内容,如有转载请申明原地址出处.谢谢 序言:what is logistic regression? Logistics 一词表示adj.逻辑的;[军]后勤学的n.[逻] ...

  8. WinForm------DockManager控件的使用方法(里面包含DockPanel控件)

    1.在"引用"中添加DevExpress.XtraBars和DexExpress.XtraNavBar程序集 2.往工具栏拖出DockManager控件,点击右上角的小三角,再点击 ...

  9. _mkdir

    [内容摘要]: C语言 在VS2013环境下使用_mkdir返回值是-,而且文件夹不存在,#include stdio.h#include direct.hmain(){)printf("无 ...

  10. jsp action中附件下载的写法

    //一些主要的包和类 import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java ...