joj 2431: Shift and Increment
2011-12-11 17:36
309 查看
2431: Shift and Increment
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
5s | 16384K | 1362 | 215 | Standard |
All operations are restricted in 0..n, that is if the result x is greater than n, it should be replace as x%n.
Input and Output
There are two integer x, n (n <=1000000)<x<n) p="" <="" line.="" in="" rules="" above="" the="" under="" 0="" to="" x="" from="" times="" minimal="" ouput="" should="" you="" each="">Sample Input
2 4 3 9
Sample Output
1 3
Problem Source: provided by skywind
#include<iostream>
#include<cstring>
using namespace std;
int a[1000003];//用来存储每个节点的数
int vis[1000003]={0,1};//用来判断每个节点的数是否被搜过。
int main()
{
int x,n;
while(scanf("%d%d",&x,&n)==2)
{
memset(vis,0,sizeof(vis));
if(x>=n)
x=x%n;
int beg=0,end=0;//这是广搜中每一层的起点和终点
int cnt=0,number=1;//cnt用来记录层数,number用来记录所有出现的数字。
a[0]=x;//第一个数
vis[x]=1;
while(!vis[0])//vis[0]==1时相当于找到了n
{
cnt++;
beg=end;
end=number;
for(int i=beg;i<end;i++)//对每一层的数进行处理
{
int k=a[i]*2;
if(k>=n)
k=k%n;
if(!vis[k])//向下一层左扩展
{
a[number++]=k;
vis[k]=1;
}
k=a[i]+1;
if(k>=n)
k%=n;
if(!vis[k])//向下一层右扩展
{
a[number++]=k;
vis[k]=1;
}
}
}
printf("%d\n",cnt);
}
return 0;
}
相关文章推荐
- joj 2431: Shift and Increment (模板队列与数组模拟队列的对比练习)
- JOJ 2431: Shift and Increment
- 2431: Shift and Increment
- JOJ 1051: Flip and Shift 解题报告
- [原创] javascript使得按左右方向键可以切换链接(模拟Tab和Shift+Tab功能)
- 解决CListBox 响应Shift和Ctrl键盘的问题
- 如何识别键盘左右的shift,Ctrl或Alt键
- JOJ 1058 T9
- word中硬回车与软回车的区别与替换·转换(shift+enter)
- JOJ 1903 1129 1157 1278
- JOJ 1052: Square Destroyer解题报告
- outlook shift+delete 邮件恢复
- jQuery ctrl+Enter shift+Enter实现代码
- JOJ 1198: Risk
- 1. 在delphi根据TshiftState值来判断用户按下Ctrl,shift,alt等键的方法
- vim的shiftwidth、tabstop、softtabstop设置
- joj 2558 Ocean Currents 特别的广搜
- 使用Emeditor转换编码(Shift-JS 到 UTF-8)
- Lucene.Net 2.9.2.2 Invalid shift value in prefixCoded string (is encoded value really an INT?) 异常排查处理
- joj1182 Lotto (求子集,深搜)