hdu - 1104 Remainder (bfs + 数论)

http://acm.hdu.edu.cn/showproblem.php?pid=1104

注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数.

而%可以得到正数和负数.

所以需要 a mod b = (a % b + b) % b 这样转换得到。

并且,由于新的N可以很大,所以我们每一步都要取%,而且最后要mod k,正常来说每步都%k就行了,但是由于其中的一个操作是N%m,所以我们每一步就不能%k了(%k%m混用会导致%出来的答案错误),而要%(k *m)(其实%(k,m的公倍数都行))。

还有就是用了vis标记数组,标记过的就不能在标记了。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <string>
  7 #include <algorithm>
  8 #include <string>
  9 #include <set>
 10 #include <functional>
 11 #include <numeric>
 12 #include <sstream>
 13 #include <stack>
 14 #include <map>
 15 #include <queue>
 16 #pragma comment(linker, "/STACK:102400000,102400000")
 17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 18 
 19 #define ll long long
 20 #define inf 0x7f7f7f7f
 21 #define lc l,m,rt<<1
 22 #define rc m + 1,r,rt<<1|1
 23 #define pi acos(-1.0)
 24 
 25 #define L(x)    (x) << 1
 26 #define R(x)    (x) << 1 | 1
 27 #define MID(l, r)   (l + r) >> 1
 28 #define Min(x, y)   (x) < (y) ? (x) : (y)
 29 #define Max(x, y)   (x) < (y) ? (y) : (x)
 30 #define E(x)        (1 << (x))
 31 #define iabs(x)     (x) < 0 ? -(x) : (x)
 32 #define OUT(x)  printf("%I64d\n", x)
 33 #define lowbit(x)   (x)&(-x)
 34 #define Read()  freopen("a.txt", "r", stdin)
 35 #define Write() freopen("b.txt", "w", stdout);
 36 #define maxn 1000000000
 37 #define N 2510
 38 #define mod 1000000000
 39 using namespace std;
 40 
 41 struct point
 42 {
 43     int num;
 44     string a;
 45 };
 46 
 47 int n,m,k,mk;
 48 int vis[1000005]; 
 49 
 50 void bfs()
 51 {
 52     memset(vis,0,sizeof(vis));
 53     point s;
 54     s.num=n,s.a="";
 55     queue<point>que;
 56     que.push(s);
 57     vis[(n%k+k)%k]=1;
 58     while(!que.empty())
 59     {
 60         point e=que.front();que.pop();
 61         if((e.num%k+k)%k==((n+1)%k+k)%k)
 62         {
 63             cout<<e.a.length()<<endl;
 64             cout<<e.a<<endl;
 65             return ;
 66         }
 67         s.num=(e.num+m)%mk;
 68         s.a=e.a+'+';
 69         if(!vis[(s.num%k+k)%k])
 70         {
 71             que.push(s);
 72             vis[(s.num%k+k)%k]=1;
 73         }
 74         s.num=(e.num-m)%mk;
 75         s.a=e.a+'-';
 76         if(!vis[(s.num%k+k)%k])
 77         {
 78             que.push(s);
 79             vis[(s.num%k+k)%k]=1;
 80         }
 81         s.num=(e.num*m)%mk;
 82         s.a=e.a+'*';
 83         if(!vis[(s.num%k+k)%k])
 84         {
 85             que.push(s);
 86             vis[(s.num%k+k)%k]=1;
 87         }
 88         s.num=(e.num%m+m)%m%mk;
 89         s.a=e.a+'%';
 90         if(!vis[(s.num%k+k)%k])
 91         {
 92             que.push(s);
 93             vis[(s.num%k+k)%k]=1;
 94         }
 95     }
 96     puts("0");
 97 }
 98 int main()
 99 {
100    // freopen("a.txt","r",stdin);
101     while(~scanf("%d%d%d",&n,&k,&m))
102     {
103         if(n==0&&m==0&&k==0) break;
104         mk=m*k;
105         bfs();
106     }
107     return 0;
108 }

 

posted @ 2015-06-03 19:10  NowAndForever  阅读(175)  评论(0编辑  收藏  举报