hdu 1104 数论+bfs
2013-07-23 12:43
274 查看
Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2122 Accepted Submission(s): 449[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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)
[align=left]Sample Input[/align]
2 2 2
-1 12 10
0 0 0
[align=left]Sample Output[/align]
0
2
*+
/* 其他没什么好说的,数字太大需要%(k*m),这个可以证明就等于对n进行+、-m操作 不影响结果,属于正常操作 */ #include<iostream> #include<queue> #include<string> using namespace std; bool vis[1000010]; struct point { int val; int step; string s; }p,t; void bfs(int n,int k,int m) { memset(vis,false,sizeof(vis)); queue<point> q; int s=((n+1)%k+k)%k; t.val=n; t.step=0; t.s=""; vis[(n%k+k)%k]=true; q.push(t); while(!q.empty()) { t=q.front(); q.pop(); if(s==(t.val%k+k)%k) { cout<<t.step<<endl; cout<<t.s<<endl; return ; } for(int i=0;i<4;i++) { p=t; p.step++; if(i==0) { p.val=(t.val+m)%(k*m); p.s+='+'; } else if(i==1) { p.val=(t.val-m)%(k*m); p.s+='-'; } else if(i==2) { p.val=(t.val*m)%(k*m); p.s+='*'; } else if(i==3) { p.val=(t.val%m+m)%m%(k*m); p.s+='%'; } if(!vis[(p.val%k+k)%k]) { q.push(p); vis[(p.val%k+k)%k]=true; } } } cout<<0<<endl; } int main() { int n,m,k; while(cin>>n>>k>>m,k || m || n) bfs(n,k,m); return 0; }
相关文章推荐
- HDU 1104 Remainder(BFS打印路径+数论)(%与mod的区别)
- hdu-1104-Remainder(BFS打印路径+数论)(%与mod的区别)
- hdu 1104/poj 2426 Remainder(数论,BFS)
- hdu 1104 数论+bfs
- HDU 1495 非常可乐(BFS||数论)
- HDU 1104 Remainder( BFS(广度优先搜索))
- HDU 1104 Remainder (BFS求最小步数 打印路径)
- HDU 1495 非常可乐(数论,BFS)
- HDOJ1104 Remainder(数论+BFS)
- HDU 1104 Remainder(BFS 同余定理)
- HDU 4294 Multiple (数论+bfs)
- hdu 1104 Remainder 【广度优先搜索+数论】
- 非常可乐 HDU - 1495 倒水问题 BFS && 数论解法
- hdu 1104 Remainder(BFS)
- HDU 1104 Remainder(BFS 同余定理)
- hdu1104 Remainder bfs找算式是否有解……
- HDU 1104 Remainder (POJ 2426 BFS+数论)
- hdu 1104 Remainder BFS
- HDU-1495 非常可乐(BFS)+数论求解
- HDU 1104 Remainder (POJ 2426 BFS+数论)