Description
给出一个整数 ,你可以对 进行两种操作。
1、将x变成4x+3
2、将x变成8x+7
问,最少通过多少次操作,使得x是1000000007的倍数?
Input
一行,一个整数x(1<=x<=1000000006)。
Output
一行,表示最少的操作步数。保证答案不超过10^5。
Hint
[数据约定]
对于50%的数据,答案不超过10
对于80%的数据,答案不超过1000
对于100%的数据,答案不超过100000
题解
想到bfs,看了看状态可能会重复所以加了hash判重
结果他们说有规律可循,微醺
code
const
p=1000000007;
maxn=1000000;
var
s:array[1..maxn]of int64;
t:array[1..maxn]of longint;
h:array[0..403219]of longint;
n:int64;
function hash(x:longint):longint;
var
i:longint;
begin
i:=x mod 403219;
while (h[i]<>0)and(h[i]<>x) do
i:=(i mod 403219)+1;
hash:=i;
end;
procedure bfs;
var
head,tail:longint;
i:int64;
begin
s[1]:=n;
head:=0;
tail:=1;
repeat
head:=(head+1);
i:=(s[head]*4 mod p+3)mod p;
if (h[hash(i)]<>i)or(i=0) then
begin
h[hash(i)]:=i;
tail:=(tail+1);
s[tail]:=i;
t[tail]:=t[head]+1;
end;
i:=(s[head]*8 mod p+7)mod p;
if (h[hash(i)]<>i)or(i=0) then
begin
h[hash(i)]:=i;
tail:=(tail+1);
s[tail]:=i;
t[tail]:=t[head]+1;
end;
until (head>=tail)or(s[head]=0)or(s[tail]=0)or(t[head]>100000);
if s[head]=0 then
writeln(t[head])
else
if s[tail]=0 then
writeln(t[tail]);
end;
begin
readln(n);
bfs;
end.