HDU 1104 Remainder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1104 Remainder相关的知识,希望对你有一定的参考价值。
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1104
Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3566 Accepted Submission(s): 828
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.
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.
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
*+
题解:
bfs求最短路,很直接,很暴力。但有些地方需要注意:
正常的想法是:
(n+m)%k=(n%k+m)%k
这样做的话问题规模会在1000。
但是“%”不像"+","-","*"一样,这个式子对“%”是不成立的也就是说:
(n%m)%k != ((n%k)%m)%k=n%k%m%k
因此为了保证n在%m操做之前不要因为%k操做而失真,我们把%k改成%km (km=k*m)
即:
(n%m)%k=(((n%km)%m))%km
ac代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<string> 6 using namespace std; 7 8 const int maxn = 1e3 + 10; 9 10 struct Node { 11 int val, step; 12 string s; 13 Node(int v, int st, string s) :val(v), step(st),s(s){} 14 Node() {} 15 }; 16 17 int n, k, m,km; 18 19 int vis[maxn*maxn]; 20 21 void bfs(int ans) { 22 queue<Node> q; 23 Node node=Node(((n%km) + km) % km, 0, ""); 24 vis[node.val] = 1; 25 q.push(node); 26 while (!q.empty()) { 27 node = q.front(); q.pop(); 28 if (node.val%k == ans) { 29 cout << node.step << endl; 30 cout << node.s << endl; 31 return; 32 } 33 int tmp = (node.val + m) % km; 34 if (!vis[tmp]) { 35 vis[tmp] = 1; 36 q.push(Node(tmp, node.step + 1, node.s + "+")); 37 } 38 tmp = ((node.val - m) % km + km) % km; 39 if (!vis[tmp]) { 40 vis[tmp] = 1; 41 q.push(Node(tmp, node.step + 1, node.s + "-")); 42 } 43 tmp = (node.val*m) % km; 44 if (!vis[tmp]) { 45 vis[tmp] = 1; 46 q.push(Node(tmp, node.step + 1, node.s + "*")); 47 } 48 tmp = (node.val%m) % km; 49 if (!vis[tmp]) { 50 vis[tmp] = 1; 51 q.push(Node(tmp, node.step + 1, node.s + "%")); 52 } 53 } 54 cout << "0" << endl; 55 } 56 57 void init() { 58 memset(vis, 0, sizeof(vis)); 59 } 60 61 int main() { 62 while (scanf("%d%d%d", &n, &k, &m) == 3) { 63 if (n == 0 && k == 0 && m == 0) break; 64 km = k*m; 65 init(); 66 bfs(((n+1)%k+k)%k); 67 } 68 return 0; 69 }
以上是关于HDU 1104 Remainder的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1104 Remainder(BFS路径记录+数论)
HDU——1788 Chinese remainder theorem again
HDU1788 Chinese remainder theorem again中国剩余定理
HDU 1788: Chinese remainder theorem again