zoukankan      html  css  js  c++  java
  • hdu1104

    Remainder

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


    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 1
    0 0 0 0
     
    Sample Output
    0
    2
    *+
    代码:

    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<iostream>
    #include<queue>
    using namespace std;
    int n,m,k,visit[1000010],t;
    typedef struct
    {
    int num;
    int step;
    string s;
    } point;
    void BFS()
    {
    int i;
    point now,next;
    now.num=n;
    now.step=0;
    now.s="";
    memset(visit,0,sizeof(visit));
    visit[(n%k+k)%k]=1;
    queue<point>q;
    q.push(now);
    while(!q.empty())
    {
    now=q.front();
    q.pop();
    if(((n+1)%k+k)%k==(now.num%k+k)%k)
    {
    printf("%d ",now.step);
    cout<<now.s<<endl;
    return ;
    }
    for(i=0; i<4; i++)
    {
    if(i==0)
    {
    next.num=(now.num+m)%t;
    next.step=now.step+1;
    next.s=now.s+'+';
    }
    else if(i==1)
    {
    next.num=(now.num-m)%t;
    next.step=now.step+1;
    next.s=now.s+'-';
    }
    else if(i==2)
    {
    next.num=(now.num*m)%t;
    next.step=now.step+1;
    next.s=now.s+'*';
    }
    else if(i==3)
    {
    next.num=(now.num%m+m)%m%t;
    next.step=now.step+1;
    next.s=now.s+'%';
    }
    if (!visit[(next.num % k + k) % k])
    {
    visit[(next.num % k + k) % k]=1;
    q.push(next);
    }
    }

    }
    printf("0 ");
    }
    int main()
    {
    while(~scanf("%d%d%d",&n,&k,&m)&&(n||m||k))
    {
    t=k*m;
    BFS();
    }
    }

  • 相关阅读:
    自制编译器 青木峰郎 笔记 Ch5 基于JavaCC的解析器描述
    自制编译器 青木峰郎 笔记 Ch4 基于JavaCC的扫描器的描述
    自制编译器 青木峰郎 笔记 Ch3 词法分析的概要
    自制编译器 青木峰郎 笔记 Ch2 Cb和Cbc
    自制编译器 青木峰郎 笔记 Ch1 开始制作编译器
    POJ 1201 Intervals 差分约束,最短路,RE会报TLE 难度:1 差分约束背景知识需联想证明
    POJ 3276 The Cow Lexicon DP 难度: 0
    POJ 1276 Cash Machine DP 难度: 0
    Java大数练习
    HDU 4344-Mark the Rope-大数素因子分解
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3256474.html
Copyright © 2011-2022 走看看