One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that he placed signs + and - between every pair of adjacent digits alternately. Now he wants to find the value of the expression he has made. Help him. 

For example, if N=12 then +1 -2 +3 -4 +5 -6 +7 -8 +9 -1+0 -1+1 -1+2 = 5

Input

Each line contains one integer number N (1≤ N ≤ 1015). Last line contains 0 and shouldn't be processed. Number of lines in the input does not exceed 40.

Output

For every line in the input write the answer on a separate line.

Example


Input:


12
0


 


Output:


5

【算法分析】

题意:

将1~N(1<=N<=10^15)写在纸上,然后在相邻的数字间交替插入+和-,求最后的结果。例如当N为12时,答案为:+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2=5。

分析:

我们先把数列出来,会找到一个规律

-1+0

-1+1

……

-1+9

-2+0

-2+1

……

-9+9

 

 

-1+0-0                  

+1-0+1

-1+0-2

+1-0+3

……

+1-9+9

-2+0-0

…………

 

如果数位为偶数,那么对应位的符号都一样,并且第一个符号为负,交替改变;

如果数位为奇数,那么每一位符号都是交错的,可以两两抵消(上下),只需计算个位即可

 

所以我们设计一下状态,需要一个pre记录上一位,以便数位为奇数时计算最后一位;一个sum表示各位数符号交替之和,用于数位为偶数时的计算;一个sub表示当前数位的符号(+/-);一个state表示数位是奇数还是偶数。(ate=0表示当前奇偶不确定,即前面枚举的数位全都是0;state=1表示当前数位为奇数;state=2表示偶数)

具体实现过程看代码

数位DP不仅可以进行数的统计,还可以进行数位的运算

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>num;
ll dp[16][10][280][3][2];
ll dfs(int pos, int pre, int sum, int state, bool sub, bool limit,bool lead)
//state=0表示当前奇偶不确定,即前面枚举的数位全都是0;state=1表示当前数位为奇数;state=2表示偶数
{
if (pos == -1)
{
if (state == 1)
return (pre&1)?pre:-pre; //pre&1==1表示为奇数,奇数只算个位
else
return sum;
}
if (!limit&&!lead && dp[pos][pre][sum+140][state][sub]!=-1)
return dp[pos][pre][sum+140][state][sub];
int up = limit?num[pos]:9;
ll tmp = 0;
for (int i = 0; i <= up; i ++)
{
int next_state = state;
if(lead==1&&i==0)
{
tmp += dfs(pos-1, i, sub?sum-i:sum+i, next_state,1, limit&&(i==up),1);
}
else
{

if (lead==1&& i != 0) //注意pos是从大到小排的,所以一个数的是奇数位还是偶数位已经确定了
{
if ((pos+1)%2==0) next_state = 2;
else next_state = 1;
}
tmp += dfs(pos-1, i, sub?sum-i:sum+i, next_state,sub^1, limit&&(i==up),lead && i==0);
//sub^1奇数-1,偶数+1,在二进制当中如果为1,则变0,如果为0,则变1
}
}

if(!limit&&!lead)
dp[pos][pre][sum+140][state][sub]=tmp;
return tmp;
}

ll cal(ll x)
{
num.clear();
ll res = 0;
if (x % 2 == 0) //如果是偶数,需要先计算一下,为这样保证奇数位上下可以相加
{
ll tmp = x;
while(tmp)
{
num.push_back(tmp%10);
tmp /= 10;
}
for (int i = (int)num.size()-1, k = -1; i >= 0; i --, k*=-1)
{
res += num[i]*k;
}
x --;
}
num.clear();
while(x)
{ num.push_back(x%10); x /= 10; }
int len = (int)num.size();
return res + dfs(len-1, 0, 0, 0, true, true,1);
}
int main()
{
ll n;
memset(dp,-1,sizeof(dp));
while(~scanf("%lld", &n))
{
if (n == 0) break;
printf("%lld\n", cal(n));
}
return 0;
}