• Remainder


    Remainder

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2133    Accepted Submission(s): 453

     

    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
    *+
     

    Author
    Wang Yijie
     

    Recommend
    Eddy

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    using namespace std;
    struct node
    {
    	int num,step;
    	string oper;
    };
    int N,K,M,KM;
    bool v[1025000];
    void bfs()
    {
    	queue<node> q;
    	while (!q.empty()) q.pop();
    	int state=((N+1)%K+K)%K;
    	memset(v,0,sizeof(v));
    	v[((N%K)+K)%K]=1;
    	node x,tmp;
    	x.num=N;
    	x.step=0;
    	x.oper="";
    	q.push(x);
    	while (!q.empty())
    	{
    		x=q.front();
    		q.pop();
    		if ((x.num%K+K)%K==state)
    		{
    			printf("%d
    ",x.step);
    			//printf("%s
    ",x.oper);
    			cout<<x.oper<<endl;
    			return;
    		}
    		tmp=x;
    		tmp.step++;
    		tmp.num=(x.num+M)%KM;
    		tmp.oper+="+";
    		if (!v[(tmp.num%K+K)%K])
    		{
    			q.push(tmp);
    			v[(tmp.num%K+K)%K]=1;
    		}
    		tmp=x;
    		tmp.step++;
    		tmp.num=(x.num-M)%KM;
    		tmp.oper+="-";
    		if (!v[(tmp.num%K+K)%K])
    		{
    			q.push(tmp);
    			v[(tmp.num%K+K)%K]=1;
    		}
    		tmp=x;
    		tmp.step++;
    		tmp.num=(x.num*M)%KM;
    		tmp.oper+="*";
    		if (!v[(tmp.num%K+K)%K])
    		{
    			q.push(tmp);
    			v[(tmp.num%K+K)%K]=1;
    		}
    		tmp=x;
    		tmp.step++;
    		tmp.num=(x.num%M)%KM;
    		tmp.oper+="%";
    		if (!v[(tmp.num%K+K)%K])
    		{
    			q.push(tmp);
    			v[(tmp.num%K+K)%K]=1;
    		}
    	}
    	printf("0
    ");
    }
    int main()
    {
    	while (scanf("%d%d%d",&N,&K,&M)!=EOF)
    	{
    		if (N+K+M==0) return 0;
    		KM=K*M;
    		bfs();
    	}
    	return 0;
    }
    

     

  • 相关阅读:
    C/C++一些库函数的实现
    约瑟夫环问题(Josephus)
    union关键字及大小端模式
    指针数组和数组指针
    巧用位运算
    C/C++生成可执行文件过程
    C语言+Modbus+NXP整体规划
    bug和待完善
    python学习第二课
    python学习第一课
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3220646.html
Copyright © 2020-2023  润新知