2431: Shift and Increment
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 5s | 16384K | 1132 | 174 | Standard |
Shift and increment is the basic operations of the ALU (Arithmetic Logical Unit) in CPU. One number can be transform to any other number by these operations. Your task is to find the shortest way from x to 0 using the shift (*=2) and increment (+=1) operations. 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)
Sample Input
2 4 3 9
Sample Output
1 3
Problem Source: provided by skywind
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=1000005;
bool flag[inf];
int a[inf];
int main()
{
int x,n,i;
while(scanf("%d%d",&x,&n)==2)
{
memset(flag,0,sizeof(flag));
int begin,end,num;
begin=end=num=0;
a[num++]=x;
flag[x]=1;
int count=0;
while(flag[0]==0)
{
end=num;
count++;
for(i=begin;i<end;i++)
{
int t=(a[i]*2)%n;
if(flag[t]==0)
{
flag[t]=1;
a[num++]=t;
}
t=(a[i]+1)%n;
if(flag[t]==0)
{
flag[t]=1;
a[num++]=t;
}
}
begin=end;
}
printf("%d/n",count);
}
return 0;
}
探讨了如何通过移位和增1操作实现从任意数到0的转换,并寻找最短的操作路径。此问题涉及CPU中算术逻辑单元的基本操作。
1240

被折叠的 条评论
为什么被折叠?



