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. PHP守护进程

    php也是可以直接进行守护进程的启动与终止的,相对于shell来说会简单很多,理解更方便,当然了php的守护进程要实现自动重启还是要依赖于shell的crontab日程表,每隔一段时间去执行一次脚本看 ...

  2. CF 363B One Bomb(枚举)

    题目链接: 传送门 One Bomb time limit per test:1 second     memory limit per test:256 megabytes Description ...

  3. PHP之:PHP编程效率的20个要点

    [导读] 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数” 用单引号 ...

  4. C++ 动态数组实例

    一维动态数组的实例: #include <iostream> using namespace std; int main() { int *arr; int n; cout<< ...

  5. JavaWeb学习总结-02 Tomcat 学习和使用

    一 Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  6. Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Objective-C 中的类和对象

    http://blog.ibireme.com/2013/11/25/objc-object/ Objective-C的runtime是开源的,源码可以在苹果官网下载到:objc4. 在objc4-5 ...

  8. uC/OS-II核心(Os_core)块

    /*************************************************************************************************** ...

  9. css007 margin padding border

    css007 margin padding border 1.理解盒模型(盒模型:就是把一些东西,包括html各种标签都包含在一个 看不见的盒子里) 1/在web浏览器中任何标签都是一个盒子,内容的周 ...

  10. CentOS7安装hive-2.1.0

    环境: CentOS7 Hadoop-2.6.4,配置两个节点:master.slave1 mysql-server 过程: 下载.解压hive-2.1.0到/usr/hadoop-2.6.4/thi ...