题目大意:求解区间[a,b]中数n满足 [n/1] + [n/2] + ... + [n/k] + ... 之后数为偶数的个数。
解题思路:这个题较之前一个找规律的规律性较强些,打表出来后,找到了规律,即数在这样一个区间,右边是偶数的平方,左边是奇数的平方-1。这样每个数开方后都是区间的一个边界值。然后直接暴力了!结果TLE了。重新推了下,发现中间的个数是一个等差数列。可以简单推一下就可以得到的。不过要注意开方后为偶数的的时候有可能,数在上一个区间内。其它详见code。
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3629
code:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define ll long long
ll ans,a,b;
ll slove(ll n){
if(n<0) return 0;
ans=0;
ll t=sqrt(n);
if(t%2){
ans+=(t+1)*t/2; //求和公式:(((t/2))(4*(t/2)+1+1))/2,依次为:(t/2)为n,1为a1,4*(t/2)+1为an
}
else{
ans+=n-t*t+1; //计算落在区间的数
ans+=t*(t-1)/2; //将奇数的公式中的t用t-1替换
}
return ans;
}
int main(){
while(~scanf("%lld%lld",&a,&b))
printf("%lld\n",slove(b)-slove(a-1));
return 0;
}