A - Jason的特殊爱好

果然如题所示、是道水题

A - Jason的特殊爱好
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Jason很喜欢数字,特别是1这个数字,因为他觉得1有特殊的含义。为了让更多的人喜欢上1,他决定出一题关于1的水题(每个人都喜欢水题)。

Input

输入数据中有多组数据,每组数据输入为两个正数,a,b(1<=a,b<=10^18)。

Output

输出a到b之间的整数包含多少个1。

Sample Input

1 1000

Sample Output

301


代码
-------------------------------------------------------------

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>

using namespace std;

long long calf(long long x)
{
long long tx = 1;
long long txx = 0;
long long ans = 0;
while (tx <= x)
{
if (tx > x / 10) txx = x + 2;
else txx = tx * 10;
long long ttx = x % txx;
if (ttx - tx >= tx)
{
ans = ans + (x / txx) * tx + tx;
}
if ((ttx - tx < tx) && (ttx >= tx))
{
ans = ans + (x / txx) * tx + x % tx + 1;
}
if (ttx < tx)
{
ans = ans + (x / txx - 1) * tx + tx;
}
// cout << tx << ' ' << txx << ' ' << ttx << endl;
tx = tx * 10;
}
return ans;
}

int main()
{
long long a , b;
while (cin >> a >> b)
{
if (a > b) cout << 0 << endl;
else cout << calf(b) - calf(a - 1) << endl;
}
}

 



posted @ 2013-05-09 22:12  ustc-acm  阅读(329)  评论(0编辑  收藏  举报