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;
    }
    

     

  • 相关阅读:
    linux上读取apk信息
    android 的混淆解析
    使用ant自动生成签名的apk
    每天一剂 WebView 良药
    看完让你觉得自己变超聪明的图
    centos7:Kafka集群安装
    centos7:storm集群环境搭建
    Mysql数据库存储数据时间与系统获取时间不一致
    centos7:ssh免密登陆设置
    FLUME安装&环境(二):拉取MySQL数据库数据到Kafka
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3220646.html
Copyright © 2011-2022 走看看