比赛 20110727 评测结果 AAWAWAWWTWAWTT
题目名称 猴子和香蕉 最终得分 35
用户昵称 Yoghurt 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-27 11:34:22
显示代码纯文本
program monkeys;
const
        filename='monkeys';
        maxb=100000;
        maxn=5000000;
type
        xx=record
                flag:boolean;
                sy,step:longint;
        end;
var
        x,y:array[0..maxn] of longint;
        que:array[0..maxn] of xx;
        n,m,q,tot:longint;

procedure init;
var
        i:longint;
begin
        readln(n,m);
        for i:=1 to m do
                readln(x[i],y[i]);
        readln(q);
end;

function check(b0:longint):boolean;
var
        head,tail,i:longint;
begin
        head:=0; tail:=1;
        que[tail].flag:=true;
        que[tail].sy:=b0;
        que[tail].step:=0;
        while head<tail do
        begin
                inc(head);
                for i:=1 to m do
                begin
                        if que[head].sy<y[i] then continue;
                        if (que[head].sy-y[i]) mod x[i]<>0 then continue;
                        inc(tail);
                        que[tail].step:=que[head].step+1;
                        que[tail].sy:=que[head].sy-y[i]-(que[head].sy-y[i]) div x[i];
                        if ((que[tail].step=n)and(que[tail].sy=0)) then exit(true);
                        if ((que[tail].step=n-1)and(que[tail].sy=0)) then exit(true);
                end;
        end;
        exit(false);
end;

procedure solve;
var
        i:longint;
begin
        tot:=0;
        for i:=1 to maxb do
                if check(i) then
                begin
                        inc(tot);
                        if tot=q then
                        begin
                                writeln(i);
                                exit;
                        end;
                end;
        writeln(-1);
end;

begin
        assign(input,filename+'.in'); reset(input);
        assign(output,filename+'.out'); rewrite(output);

        init;
        solve;

        close(input); close(output);
end.