Description
It's year 2100. Electricity has become very expensive. Recently, your electricity company raised the power rates once more. The table below shows the new rates (consumption is always a positive integer):
Range | Price |
(Crazy-Watt-hour) | (Americus) |
1 100 | 2 |
101 10000 | 3 |
10001 1000000 | 5 |
> 1000000 | 7 |
This means that, when calculating the amount to pay, the first 100 CWh have a price of 2 Americus each; the next 9900 CWh (between 101 and 10000) have a price of 3 Americus each and so on.
For instance, if you consume 10123 CWh you will have to pay 2 x 100 + 3 x 9900 + 5 x 123 = 30515 Americus.
The evil mathematicians from the company have found a way to gain even more money. Instead of telling you how much energy you have consumed and how much you have to pay, they will show you two numbers related to yourself and to a random neighbor:
-
A:
- the total amount to pay if your consumptions were billed together; and B:
- the absolute value of the difference between the amounts of your bills.
If you can't figure out how much you have to pay, you must pay another 100 Americus for such a ``service". You are very economical, and therefore you are sure you cannot possibly consume more than any of your neighbors. So, being smart, you know you can compute how much you have to pay. For example, suppose the company informed you the following two numbers: A = 1100 and B = 300. Then you and your neighbor's consumptions had to be 150 CWh and 250 CWh respectively. The total consumption is 400 CWh and then A is 2 x 100 + 3 x 300 = 1100. You have to pay 2 x 100 + 3 x 50 = 350 Americus, while your neighbor must pay 2 x 100 + 3 x 150 = 650 Americus, soB is | 350 - 650| = 300.
Not willing to pay the additional fee, you decided to write a computer program to find out how much you have to pay.
Input
The input contains several test cases. Each test case is composed of a single line, containing two integers A and B, separated by a single space, representing the numbers shown to you (1A, B109). You may assume there is always a unique solution, that is, there exists exactly one pair of consumptions that produces those numbers.
The last test case is followed by a line containing two zeros separated by a single space.
Output
For each test case in the input, your program must print a single line containing one integer, representing the amount you have to pay.
Sample Input
1100 300 35515 27615 0 0
Sample Output
350 2900
#include<stdio.h>
#include<string.h>
#define INF 1<<30
int f[9]={0,0,0,200,0,29900,0,4979900};
int min(int a,int b)
{
return a<b?a:b;
}
int chu(int k)
{
if(k==2)
return 0;
if(k==3)
return 100;
if(k==5)
return 10000;
else
return 1000000;
}
int jie(int k)
{
if(k==2)
return 100;
if(k==3)
return 10000;
if(k==5)
return 1000000;
else
return INF;
}
int du(int m)
{
int i,j,h;
h=0;
if(m<=200)
{
h+=m/2;
return h;
}
if(m<=29900)
{
h+=100+(m-200)/3;
return h;
}
if(m<=4979900)
{
h+=10000+(m-29900)/5;
return h;
}
else
{
h+=1000000+(m-4979900)/7;
return h;
}
return -1;
}
int fanw(int v)
{
if(v<=100)
return 2;
if(v<=10000)
return 3;
if(v<=1000000)
return 5;
else
return 7;
}
int pay(int r)
{
int i,j,k,l,h;
h=0;
k=fanw(r);
h+=f[k]+k*(r-chu(k));
return h;
}
int end,a,b;
void dfs(int ta,int tb,int dif)
{
if(dif==b)
{
end=ta;
return ;
}
if(dif>b)
{
return;
}
int ka,kb,cha,chb,tmp,i,j,k,l;
ka=fanw(ta);
kb=fanw(tb);
cha=ta-chu(ka);
chb=jie(kb)-tb;
if(cha==0||chb==0) //到头了,tmp是0再算没意义,先退一步
{
dfs(ta-1,tb+1,dif+fanw(ta-1)+fanw(tb+1));
return;
}
tmp=min(cha,chb);
l=dif;
l+=tmp*(ka+kb); //补全之后的差值
if(l==b) //补完刚好相等
{
end=ta-tmp;
return;
}
else if(l<b) //补全也不够,退tmp
{
ta=ta-tmp;
tb=tb+tmp;
dif+=(ka+kb)*tmp;
dfs(ta,tb,dif);
}
else if(l>b) //补全抄了,说明就在这个范围里出结果;
{
end=ta-(b-dif)/(ka+kb);
return;
}
}
int main()
{
int i,j,k,l,m,n,cha,flag;
int sum,ta,tb;
while(scanf("%d %d",&a,&b)&&a+b)
{
sum=du(a);
ta=sum/2;
tb=sum-sum/2;
if(ta==tb)
cha=0;
else
cha=1;
int dif=0;
int tmp=0;
dfs(ta,tb,0);
printf("%d\n",pay(end));
}
while(0)
{
scanf("%d",&a);
printf("%d\n",du(a));
}
return 0;
}