电费结算

Description

WZK最近靠租房发家致富了。作为WZK老同学的你也要租房,于是WZK决定不要房租,但是电
费还得付。以下是用电价格:
举个例子吧。如果你用电为10123千瓦时,那么要付2 * 100 + 3 * 9900 + 5 * 123 = 30515块钱(好贵)。
到结算电费的日子了,可是WZK家里只有一个总电表,也就是统计你和WZK总共用的电量。
是WZK有办法告诉你以下信息:
1).如果按照总电表来看要交给供电局的钱A。(也就是两个人用电量加起来一起算钱)
2).你和WZK如果分开付的话,你们付的钱的差值B。
现在你想知道如果你单独算钱的话,需要付多少钱。当然,你的用电量不会比WZK多。
举个例子:如果你们一起算钱要付1100,并且如果分开来算,你们的差值是300的话,那么你用了150kwh,WZK用了250kwh。让我们来验算一下:你们一共用电400kwh,所以要付2 * 100 + 3 * 300 = 1100,你单独要付2 * 100 + 3 * 50 = 350,WZK单独要付2 * 100 + 3 * 150 = 650。所以最后,你只需要告诉我你单独要付350元。

Input

输入仅一行,包含两个整数A和B(1 ≤ A, B ≤ 10^9),含义同上。 输出描述: 输出仅一
行一个整数,代表你单独算需要付的钱。数据保证解唯一。

Output

输出仅一行一个整数,代表你单独算需要付的钱。数据保证解唯一。

Sample Input

1100 300

Sample Output

350



思路:一道二分题,先算出两人一共花费的用电量
再二分用电量,原因是由于具有单调性
贴上代码(写的丑勿怪)


const
c1=200;
c2=29700;
c3=4950000;
var
a,b,sum1,sum2,s,pre,re,mid,i,j,k,n:longint;
function solve(x:longint):longint;
begin
    if x<=c1 then
    exit(x div 2);
    if x<=c1+c2 then
    exit((x-c1) div 3+100);
    if x<=c1+c2+c3 then
    exit((x-c1-c2) div 5+10000);
    exit((x-c1-c2-c3) div 7+1000000);
end;
function sum(x:longint):longint;
begin
    if (x<=100) then
    exit(x*2);
    if (x<=10000) then
    exit((x-100)*3+c1);
    if (x<=1000000) then
    exit((x-10000)*5+c1+c2);
    exit((x-1000000)*7+c1+c2+c3)
end;
begin
    read(a,b);
    pre:=0;
    s:=solve(a);
    re:=s;
    while pre<re do
    begin
        mid:=(pre+re) div 2;
        sum1:=sum(mid);
        sum2:=sum(s-mid);
        if sum2-sum1=b then
        begin
            writeln(sum1);
            halt;
        end
        else
        if sum2-sum1>b then
        pre:=mid+1
        else re:=mid;
    end;
end.
View Code


 
原文地址:https://www.cnblogs.com/fhlxpyz/p/6024392.html