zoukankan      html  css  js  c++  java
  • HDU 1104 Remainder(BFS路径记录+数论)

    Remainder

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


    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   |   We have carefully selected several similar problems for you:  1195 1016 1175 1180 1043
     
    以下有部分引用kuangbin大神
            分析:题目要求最小步数,很容易想到用BFS,这里注意一下
                %与mod的区别:%出来的数有正有负,符号取决于左操作数。。。而mod只能是正(因为a = b * q + r (q > 0 and 0 <= r < q), then we have a mod q = r    中r要大于等于0小于q)。

               所以要用%来计算mod的话就要用这样的公式:a mod b = (a % b + b) % b

                括号里的目的是把左操作数转成正数

               要让BFS记录的状态尽量的少,常常会想到在每个地方都%k,记录每次%k后的值
               但是这题会涉及到%k和%m,
               如果记n = 2, m = 8, k =3. 则((n * m)%k % m)%k = 1 而(n * m % m)%k = 0。所以我们不能将每一步记录的值都%k;
              为了不影响后面 所以我们将%km(这里其实用k和m的公倍数就可以了)后的值进行状态记录,关于路径记录则用string进行
             
            
    代码如下:
    
    
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include <cstring>
    #include <queue>
    #include <map>
    using namespace std;
    typedef long long ll;
     int  n,k,m;
     int km;
    int vis[1000100];
    struct node
    {
        int x;
        int  step;
        string path;
        int num;
    };
    queue<node>Q;
    node bfs()
    {
        int p,q;
      node a,next,fail;
      a.x=n;
      a.step=0;
      Q.push(a);
      q=((n+1)%k+k)%k;
      while(!Q.empty())
      {
       a=Q.front();
       Q.pop();
       p=(a.x%k+k)%k;
        // cout<<p<<" "<<q<<endl;
       if(p==q){
     //   cout<<p<<endl;
       return a;
       }
        next.step=a.step+1;
        next.x=((a.x+m)%km+km)%km;
        next.path=a.path+'+';
        if(!vis[next.x])
        {
            vis[next.x]=1;
            Q.push(next);
        }
    
         next.x=((a.x-m)%km+km)%km;
        next.path=a.path+'-';
        if(!vis[next.x])
        {
            vis[next.x]=1;
            Q.push(next);
        }
    
          next.x=(a.x*m%km+km)%km;
        next.path=a.path+'*';
        if(!vis[next.x])
        {
            vis[next.x]=1;
            Q.push(next);
        }
               next.x=(a.x%m+m)%m%km;
        next.path=a.path+'%';
        if(!vis[next.x])
        {
            vis[next.x]=1;
            Q.push(next);
        }
    
      }
      fail.step=0;
      return fail;
    }
    int main()
    {
      //   freopen("1.out","w",stdout);
        while(scanf("%d%d%d",&n,&k,&m)!=EOF){
        if(n==0&&k==0&&m==0)break;
             km=m*k;
           node ans;
         memset(vis,0,sizeof(vis));
           while(!Q.empty())Q.pop();
           ans=bfs();
           if(ans.step==0)puts("0");
           else{
              printf("%d
    ",ans.step);
              cout<<ans.path<<endl;}
       }
    
       return 0;
    }
     
           
             
  • 相关阅读:
    Html5 Input 类型
    Html5 部分特性
    Asp.net Mvc4 基于Authorize实现的模块访问权限
    第11天知识点总结
    C# string类型和byte[]类型相互转换
    C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法
    Socket 学习
    C#中的Dictionary字典类介绍
    js判断客户端是pc还是手机
    input type="file" accept="image/*"上传文件慢的问题解决办法
  • 原文地址:https://www.cnblogs.com/a249189046/p/7520496.html
Copyright © 2011-2022 走看看