hdu 1104poj 2426Remainder(数论,BFS)

    xiaoxiao2023-05-26  7

    Remainder

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3849    Accepted Submission(s): 925 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 *+   题意:给你n,k,m,问n经过几次操作后%k会等于初始(n+1)%k,操作可以是对m做+-*%

    思路:显然的搜索题,直接BFS,不过问题在于我们不能步步对k取模,因为如果过程有%m%k结果不会跟总结果%k相等

    那么怎么办呢?  没关系,我们每步都对m*k取模,然后最后对k取模即可。正确性显然。

    还有一个问题,我一开始没注意到,被坑了一个小时啊.......

    You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.

    取模的结果>=0,也就是不会出来负数,这点要注意。

    得到的是取模后最小的整数

    代码:

    #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cmath> #include <queue> using namespace std; long long n,m,k; int vis[1000005]; struct Node { string s; int step; long long num; }; int bfs() { Node a,next; a.step=0; a.num=n; a.s=""; queue<Node>que; que.push(a); memset(vis,0,sizeof(vis)); vis[(n%(m*k)+(m*k))%(m*k)]=1; while(!que.empty()) { Node now=que.front(); que.pop(); if((now.num%k+k)%k==((n+1)%k+k)%k) { printf("%d\n",now.step); cout<<now.s<<endl; return 1; } next=now; next.step=now.step+1; next.num=(next.num+m)%(m*k); next.num=(next.num+(m*k))%(m*k); next.s+="+"; if(!vis[next.num]) { vis[next.num]=1; que.push(next); } next=now; next.step=now.step+1; next.num=(next.num-m)%(m*k); next.num=(next.num+(m*k))%(m*k); next.s+="-"; if(!vis[next.num]) { vis[next.num]=1; que.push(next); } next=now; next.step=now.step+1; next.num=(next.num*m)%(m*k); next.num=(next.num+(m*k))%(m*k); next.s+="*"; if(!vis[next.num]) { vis[next.num]=1; que.push(next); } next=now; next.step=now.step+1; next.num=(next.num%m+m)%m%(m*k); next.num=(next.num+(m*k))%(m*k); next.s+="%"; if(!vis[next.num]) { vis[next.num]=1; que.push(next); } } return -1; } int main() { while(~scanf("%lld %lld %lld",&n,&k,&m)&&(n||m||k)) { if(bfs()==-1) printf("0\n"); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1260654.html
    最新回复(0)