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. vc++ 中 IntelliSense: 无法打开 源 文件 "xxx.h"

    类似无法找到文件的问题都可以用这个方法解决,就是路径的问题.vc++2008的项目转到vc++2010也可能出现类似的问题. 解决方法: 在  项目属性=>配置属性=>C/C++  =&g ...

  2. 屠蛟之路_蛟灵岛战役(上)_SixthDay

    乘风破浪,屠蛟少年们终于到达beta怪蛟大boss的老巢--蛟灵岛. 这是一座孤立在东海深处的荒岛,岛上黑烟缭绕.瘴气重重,屠蛟少年们一登岛,就感受到浓浓的腥味和妖气. 果然,再小心翼翼,走两步居然陷 ...

  3. HTML5学习总结-01 开发环境和历史

    1 搭建HTML5开发环境 1 安装一款支持HTML5的浏览器 FireFox, Chrome 2 开发工具 SublineText, Eclipse, HBuilder, WebStorm 注:使用 ...

  4. JavaScript格式化日期

    查找格式化日期的方法大都是写日期扩展方法,也许是为了维持jquery easyUI 源码完整性, // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h ...

  5. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  6. 最近这么火的iOS视频直播

    快速集成iOS基于RTMP的视频推流 http://www.jianshu.com/p/8ea016b2720e iOS视频直播初窥:高仿<喵播APP> http://www.jiansh ...

  7. IsPostBack--Asp.net

    .net程序员首先需要了解什么是IsPostBack.msdn上边有IsPostBack的定义:获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问.如果是为响应客户端回 ...

  8. HTTP 学习

    *** *** http://www.w3school.com.cn/xml/xml_http.asp *** *** http://www.cnblogs.com/shenliang123/arch ...

  9. Oracle 中count(1) 和count(*) 的区别

    count()与count(*)比较: 如果你的数据表没有主键,那么count()比count(*)快 如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快 如果你的表只有一 ...

  10. 性能:15个JavaScript本地存储技术的函数库和工具

    当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...