空降題目處(外網)
點我點我點我
空降題目處(內網)
點我點我點我
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
輸出僅一行一個整數,代表你單獨算需要付的錢。數據保證解唯一。
Solution
利用
Code
C++
#include<fstream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int a,b,n;
int bs(int l,int r);
int main()
{
//freopen("init.in","r",stdin);
scanf("%d%d",&a,&b);
if (a>4979900)
n=(a-4979900)/7+1000000;
else
if (a>29900)
n=(a-29900)/5+10000;
else
if (a>200)
n=(a-200)/3+100;
else
n=a/2;
int t=n-bs(n/2,n);
printf("%d",min(t,100)*2+(max(min(t,10000),100)-100)*3+(max(min(t,1000000),10000)-10000)*5+(max(min(t,1000000000),1000000)-1000000)*7);
}
int bs(int l,int r)
{
if (l>=r)
return l;
int m=(l+r)/2;
if ((min(m,100)*2+(max(min(m,10000),100)-100)*3+(max(min(m,1000000),10000)-10000)*5+(max(min(m,1000000000),1000000)-1000000)*7)-b<(min((n-m),100)*2+(max(min((n-m),10000),100)-100)*3+(max(min((n-m),1000000),10000)-10000)*5+(max(min((n-m),1000000000),1000000)-1000000)*7))
return bs(m+1,r);
else
return bs(l,m);
}
Pascal
var
n,m,i,j,k,x,y,t,l,r,mid:longint;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end;
begin
read(n,m);
if n>4979900 then t:=(n-4979900) div 7+1000000 else
if n>29900 then t:=(n-29900) div 5+10000 else
if n>200 then t:=(n-200) div 3+100 else t:=n div 2;
l:=1;
r:=t div 2;
while l<r do
begin
mid:=(l+r) div 2;
if mid>1000000 then x:=(mid-1000000)*7+4979900 else
if mid>10000 then x:=(mid-10000)*5+29900 else
if mid>100 then x:=(mid-100)*3+200 else x:=mid*2;
k:=t-mid;
if k>1000000 then y:=(k-1000000)*7+4979900 else
if k>10000 then y:=(k-10000)*5+29900 else
if k>100 then y:=(k-100)*3+200 else y:=k*2;
if y-x=m then
begin
write(x);
halt;
end;
if y-x>m then l:=mid+1 else r:=mid;
end;
end.