BILL - Hóa đơn tiền điện

Tác giả: ladpro98

Ngôn ngữ: Pascal

program bill;
uses    math;
type    e=record
        val:int64;
        full:boolean;
        end;
const   fi='';
        lim:array[1..3] of longint = (100,10000,1000000);
        limP:array[1..3] of longint = (20000,2970000,495000000);
        p:array[1..4] of longint = (200,300,500,700);
var     x,y:int64;
        inp:text;

function KwhToMoney(a:int64):int64;
var     s:int64;
begin
        if a<=lim[1] then exit(p[1]*a);
        s:=lim[1]*p[1];
        dec(a,lim[1]);
        if a<=(lim[2]-lim[1]) then exit(s+p[2]*a);
        inc(s,(lim[2]-lim[1])*p[2]);
        dec(a,lim[2]-lim[1]);
        if a<=(lim[3]-lim[2]) then exit(s+p[3]*a);
        inc(s,(lim[3]-lim[2])*p[3]);
        dec(a,(lim[3]-lim[2]));
        exit(s+p[4]*a);
end;

function MoneyToKwh(a:int64):e;
var     s:int64;
        tp:e;
begin
        if a<=limP[1] then
        begin
                if a mod p[1]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=a div p[1];
                exit(tp);
        end;
        s:=lim[1];
        dec(a,limP[1]);
        if a<=limP[2] then
        begin
                if a mod p[2]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[2];
                exit(tp);
        end;
        s:=lim[2];
        dec(a,limP[2]);
        if a<=limP[3] then
        begin
                if a mod p[3]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[3];
                exit(tp);
        end;

        s:=lim[3];
        dec(a,limP[3]);
        if a mod p[4]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[4];
                exit(tp);
end;

procedure process;
var     l,r,t,m:int64;
        a,b:e;
begin
        l:=0;r:=x;
        while (l<=r) do
        begin
                m:=(l+r) div 2;
                a:=MoneyToKwh(m);
                b:=MoneyToKwh(m+y);
                t:=KwhToMoney(a.val+b.val);
                if t=x then
                if a.full and b.full then
                begin
                        write(m);
                        exit;
                end
                else    r:=m-1;
                if t<x then l:=m+1
                else    r:=m-1;
        end;
end;

begin
        assign(inp,fi);reset(inp);
        readln(inp,x,y);
        process;

end.

Download