zoukankan      html  css  js  c++  java
  • 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;
    }
    

     

  • 相关阅读:
    SAP系统管理中常见问题解答(转载)
    STMS传输队列中的请求状态一直是Running不能结束
    通过Tcode查找Badi或者客户出口
    通过LDB_PROCESS函数使用逻辑数据库
    ABAP读取长文本的方法
    SAP增强Enhancement
    ABAP开发顾问必备:SAP ABAP开发技术总结[转载]
    SAP事件 Event Flow(转载)
    jQuery中的text()、html()和val()以及javaScript中的innerText、innerHTML和value
    js访问对象属性的方式“.”与“[]”的区别
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3220646.html
Copyright © 2011-2022 走看看