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. 根据html容器大小和显示文字多少调节字体大小

    在做html相关的东西的时候经常会遇到这样的问题,容器大小(长x宽)固定,容器包含内容(特指文字)多少不固定,这个时候就让人很苦恼了,将字体大小设置成多少才合适呢?下面看看我的解决思路: 首先要知道网 ...

  2. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  3. 更改primefaces theme

    PrimeFaces is using jQuery ThemeRoller CSS theme framework, and come with 30+ pre-designed themes th ...

  4. HTML5学习总结-11 IOS 控件WebView显示网页

    一 加载外部网页 1.使用UIWebView加载网页 运行XCode  新建一个Single View Application . 2 添加安全消息 添加以下消息到项目的  Info.plist &l ...

  5. 【原】javascript最佳实践

    摘要:这篇文章主要内容的来源是<javascript高级程序设计第三版>,因为第二遍读完,按照书里面的规范,发觉自己在工作中没有好好遵守.所以此文也是对自己书写js的一种矫正. 1.可维护 ...

  6. nginx 学习笔记(9) 配置HTTPS服务器--转载

    HTTPS服务器优化SSL证书链合并HTTP/HTTPS主机基于名字的HTTPS主机带有多个主机名的SSL证书主机名指示兼容性 配置HTTPS主机,必须在server配置块中打开SSL协议,还需要指定 ...

  7. sp_executesql 使用

    sp_executesql 比 之前的exec @sql 区别在可以实现参数的传入传出 如 declare @sql nvarchar(2000) declare @pid varchar(20) s ...

  8. TemplateDataField

    .aspx <ig:TemplateDataField Key="TemplateField_0"> <Header Text="selected&qu ...

  9. 自然语言18_Named-entity recognition

    https://en.wikipedia.org/wiki/Named-entity_recognition http://book.51cto.com/art/201107/276852.htm 命 ...

  10. js操作DOM动态添加和移除事件

    非IE下,注意事件名不带on,如onclick为click 添加事件:DOM对象.addEventListener('事件名',函数名,true/false); 删除事件:DOM对象.removeEv ...