hdu 1104 数论+bfs
2014-04-30 09:04
309 查看
题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k 输出最小路径与运算副
zsd:% 只是求余数 有正负 mod 是求模 无正负、
yhd:对m*k求余对 对k求余不对
zsd:% 只是求余数 有正负 mod 是求模 无正负、
yhd:对m*k求余对 对k求余不对
#include<iostream> #include<queue> using namespace std; struct Node { int x; int step; queue<char> q; }; int v[1000010]; int n,m,k; bool bfs() { int s=((n+1)%k+k)%k; queue<Node>q; Node now,next; now.x=n;now.step=0; q.push(now); memset(v,0,sizeof(v)); while(!q.empty()) { now=q.front(); if((now.x%k+k)%k==s) { printf("%d\n",now.step); while(now.step--) { printf("%c",now.q.front()); now.q.pop(); } printf("\n"); return true; } q.pop(); int xx; xx=(now.x+m)%(k*m); if(!v[(xx%k+k)%k]) { v[(xx%k+k)%k]=1; next=now;next.x=xx;next.q.push('+');next.step++; q.push(next); } xx=(now.x-m)%(k*m); if(!v[(xx%k+k)%k]) { v[(xx%k+k)%k]=1; next=now;next.x=xx;next.q.push('-');next.step++; q.push(next); } xx=(now.x*m)%(k*m);//因为xx会变得很大 所以要求余变小 if(!v[(xx%k+k)%k]) { v[(xx%k+k)%k]=1; next=now;next.x=xx;next.q.push('*');next.step++; q.push(next); } xx=((now.x%m+m)%m)%(m*k); if(!v[(xx%k+k)%k]) {v[(xx%k+k)%k]=1; next=now;next.x=xx;next.q.push('%');next.step++; q.push(next); } } return 0; } int main() { while(scanf("%d%d%d",&n,&k,&m)!=EOF) { if(n==0&&k==0&&m==0) break; if(!bfs()) printf("0\n"); } return 0; }
相关文章推荐
- HDU 1104 Remainder(BFS打印路径+数论)(%与mod的区别)
- hdu 1104 数论+bfs
- hdu 1104/poj 2426 Remainder(数论,BFS)
- hdu-1104-Remainder(BFS打印路径+数论)(%与mod的区别)
- HDU 1495 非常可乐(BFS||数论)
- HDU 1104 Remainder (BFS求最小步数 打印路径)
- HDU 1104 Remainder( BFS(广度优先搜索))
- HDU 1495 非常可乐(数论,BFS)
- HDOJ1104 Remainder(数论+BFS)
- hdu 1104 Remainder 【广度优先搜索+数论】
- HDU 1104 Remainder(BFS 同余定理)
- HDU 4294 Multiple (数论+bfs)
- 非常可乐 HDU - 1495 倒水问题 BFS && 数论解法
- hdu 1104 Remainder(BFS)
- HDU 1104 Remainder(BFS 同余定理)
- hdu1104 Remainder bfs找算式是否有解……
- HDU 1104 Remainder (POJ 2426 BFS+数论)
- HDU-1495 非常可乐(BFS)+数论求解
- HDU 1104 Remainder (POJ 2426 BFS+数论)
- HDU 1104 BFS 队列