/ Vijos / 讨论 / 循环 /

。_ ) 好难...

v1算法对了两个点...v2只对一个...

v1:
// Amphineko's Code

var
Base: Cardinal;
PartLength: Byte;
PartDiv: Cardinal;

function StackFunc(const Depth: Byte; const LastPart: Cardinal): Integer;
var
CurrentPart: Cardinal;
begin
CurrentPart := LastPart * Base mod PartDiv;
if CurrentPart = Base then
begin
StackFunc := Depth;
Exit;
end;
if Depth = 100 then
begin
StackFunc := -1;
Exit;
end;
StackFunc := StackFunc(Depth + 1, CurrentPart);
end;

procedure Prepare;
var
loop1: Byte;
begin
PartDiv := 1;
for loop1 := 1 to PartLength do
PartDiv := PartDiv * 10;
Base := Base mod PartDiv;
end;

begin
Readln(Base, PartLength);
Prepare;
Write(StackFunc(2, Base * Base mod PartDiv));
end.

v2:
// Amphineko's Code

var
Base: Cardinal;
PartLength: Byte;
PartDiv: Cardinal;
Lens: array [1..10] of Byte;
LenPos: Byte;

function JudgeRepeat: Boolean;
var
loop1: Byte;
begin
JudgeRepeat := False;
if (LenPos - 1) mod 2 = 0 then
begin
for loop1 := 1 to LenPos div 2 do
if Lens[loop1] <> (Lens[loop1 + LenPos div 2] div 2) then
Exit;
JudgeRepeat := True;
end;
end;

function JudgeFunc(const CurrentPart, Depth: Cardinal): Boolean;
begin
if CurrentPart = Base then
begin
Lens[LenPos] := Depth;
Inc(LenPos);
if LenPos > 2 then
begin
JudgeFunc := JudgeRepeat;
Exit;
end;
end;
JudgeFunc := False;
end;

function StackFunc(const Depth: Byte; const LastPart: Cardinal): Integer;
var
CurrentPart: Cardinal;
begin
CurrentPart := LastPart * Base mod PartDiv;
if JudgeFunc(CurrentPart, Depth) then
begin
StackFunc := Depth div 2;
Exit;
end;
if Depth = 400 then
begin
StackFunc := -1;
Exit;
end;
StackFunc := StackFunc(Depth + 1, CurrentPart);
end;

procedure Prepare;
var
loop1: Byte;
begin
PartDiv := 1;
for loop1 := 1 to PartLength do
PartDiv := PartDiv * 10;
Base := Base mod PartDiv;
LenPos := 1;
end;

begin
Readln(Base, PartLength);
Prepare;
Write(StackFunc(2, Base * Base mod PartDiv));
end.

6 条评论

  • 1

信息

ID
1032
难度
7
分类
高精度 点击显示
标签
递交数
4103
已通过
877
通过率
21%
被复制
35
上传者