HDU - 1104 Remainder 搜索
来源:互联网 发布:魔方虚拟光驱软件下载 编辑:程序博客网 时间:2024/06/06 07:18
本题是bfs,不过有点小阴谋,在num因为大量运算时会很大,于是我们要取余,但不能直接用%k,而是用%km(k*m),因为不止要和k线性同余还要和m线性同余,也就是说,如果只用%k,那么我对n做了一步处理后,产生的结果只对k线性同余,假如下一步用%m,那么就错了比如,8 3,10,op为+%,如果用%k,答案就是2,但正确答案是1。
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<vector>#include<queue>#include<algorithm>using namespace std;const int maxn = 1005;const int inf = 1<<30;int n,k,m,ans;bool vis[maxn*maxn];struct node{int num;string str;};void BFS(){node cur,cnt;int km = k*m;ans = ((n+1)%k+k)%k;memset( vis,0,sizeof(vis) );queue<node>que;vis[(n%k+k)%k] = true;cur.num = n;que.push(cur);while( !que.empty() ){cur = que.front(); que.pop();for( int i = 1; i <= 4; i ++ ){if( i == 1 ){cnt.num = (cur.num + m)%km;cnt.str = cur.str + '+';}else if ( i == 2 ){cnt.num = (cur.num - m)%km;cnt.str = cur.str + '-';}else if ( i == 3 ){cnt.num = (cur.num * m)%km;cnt.str = cur.str + '*';} else{cnt.num = ((cur.num%m+m)%m)%km;cnt.str = cur.str + '%';}if( (cnt.num%k+k)%k == ans ){cout<<cnt.str.length()<<endl;cout<<cnt.str<<endl;return;}if( !vis[(cnt.num%k+k)%k] ){vis[(cnt.num%k+k)%k] = true;que.push(cnt);}}}cout<<0<<endl;}int main(){//freopen("data.txt","r",stdin);while( scanf("%d%d%d",&n,&k,&m) == 3,(n||k||m) ){BFS();}return 0;}
- HDU - 1104 Remainder 搜索
- hdu 1104 Remainder 【广度优先搜索+数论】
- HDU 1104 Remainder( BFS(广度优先搜索))
- HDU 1104 Remainder
- hdu 1104 Remainder
- hdu 1104 Remainder BFS
- Hdu 1104 Remainder[Bfs]
- HDU 1104 Remainder
- HDU 1104 Remainder
- Remainder hdu 1104
- hdu 1104 Remainder 队列
- 【bfs】hdu 1104 Remainder
- hdu 1104 Remainder(BFS)
- HDU 1104 remainder
- 【HDU】 1104 Remainder
- hdu 1104 remainder bfs+数论
- HDU 1104 Remainder (BFS)
- HDU 1104题Remainder(bfs)
- net网站提高性能的方法
- CoreMotion框架-iOS设备的核心运动
- 关于java类的封装性的一点看法
- js输出字符串
- Content Provider
- HDU - 1104 Remainder 搜索
- Given final block not properly padded
- Tesseract-OCR 字符识别---样本训练
- 解决Oracle 11g空表不能导出
- gcc原子操作,AtomicIntegerT , gcc编译选项(11)
- 再谈OpenCV数据结构Mat详解
- 狄拉克δ函数
- Objective-C编码规范[不定期更新细节]
- Msys/MinGW与Cygwin/gcc