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. Unity3d5.0 新UI之2048

    因为汽车系统没写出来所以,纠结之中,弄了弄新版本的UI. 做了个2048. 新版本的unity的UI必须以Canvas为基底来呈现,如果没有加画布的话可是显示不出来东西的哦. 而且作为UI上的所有组件 ...

  2. 有关Java的日期处理的一些杂记

    在企业应用开发中,经常会遇到日期的相关处理,说实话JDK自带的日期方法很难用.就我个人而言我一般都会采用joda-time来替代JDK自身的日期. 这篇文章是杂记,所以写的比较零散,希望大家不要见怪. ...

  3. Oracle 子查询

    1.子查询在SELECT.UPDATE.DELETE语句内部可以出现SELECT语句.内部的SELECT语句结果可以作为外部语句中条件子句的一部分,也可以作为外部查询的临时表.子查询的类型有: ① 单 ...

  4. 强连通分量的Tarjan算法

    资料参考 Tarjan算法寻找有向图的强连通分量 基于强联通的tarjan算法详解 有向图强连通分量的Tarjan算法 处理SCC(强连通分量问题)的Tarjan算法 强连通分量的三种算法分析 Tar ...

  5. 【Alpha阶段】第二次Scrum例会

    燃尽图软件存在bug,正在排查修复:(已修复)由于时区设置到了美国,图表显示有问题. 会议信息 时间:2016.10.18 22:00 时长:1h 地点:大运村1号公寓5楼楼道 类型:设计阶段阶段性会 ...

  6. Spring 通过maven pom文件配置初始化

    spring对bean的生命周期管理的比较精细,并不是单纯的new()实例化. 1,找到class配置信息并将其实例化 2,受用依赖注入,按照配置信息,配置bean的所有属性; 在一个开始使用前可以用 ...

  7. 闭包->类的实例数组排序

    简单的字符串数组排序就一句话 Arr.sort(function(s1,s2){return s1.localeCompare(s2));//升序 Arr.sort(function(s1,s2){r ...

  8. 自然语言20_The corpora with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...

  9. C# “配置系统未能初始化” 异常解决

    使用App.config配置参数,读取参数出现错误 “System.Configuration.ConfigurationErrorsException”类型的未经处理的异常在 System.Conf ...

  10. Java数据结构与排序算法——堆和堆排序

    //================================================= // File Name : Heap_demo //--------------------- ...