天天看點

UVa 10994 Simple Addition (組合數學)

10994 - Simple Addition

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1935

計算sum{i從右往左數的第一個非0數字,p<=i<=q}。

見代碼。。

/*0.016s*/

#include<cstdio>
typedef long long ll;

ll sum(ll n)
{
	ll ans = 0, x;
	while (n)
	{
		x = n % 10;
		n /= 10;
		ans += ((1 + x) * x) / 2 + n * 45;///當個位在循環的時候,高位的朋友你在幹嘛?
	}
	return ans;
}

int main()
{
	ll a, b;
	while (scanf("%lld%lld", &a, &b), a >= 0)
		printf("%lld\n", sum(b) - sum(a - 1));
}