您的位置:首页 > 其它

zoj 3629 Treasure Hunt IV(找规律)

2015-10-27 14:06 225 查看
Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a fromb in front of her.
Alice was very excited but unfortunately not all of the treasures are real, some are fake.
Now we know a treasure labled n is real if and only if [n/1] + [n/2] + ... + [n/k] + ... is even.
Now given 2 integers a and b, your job is to calculate how many real treasures are there.

Input

The input contains multiple cases, each case contains two integers a and b (0 <= a <= b <= 263-1) seperated by a single space. Proceed to the end of file.

Output

Output the total number of real treasure.

Sample Input

0 2
0 10


Sample Output

1
6

一开始还考虑用欧拉定理算因子和什么的。。。
然后看到数据范围。。。噗。。显然不能那样做。。。
所以一开始看清楚数据范围是很重要的。

这个数据范围应该就是找规律了。。。。
结果试着找了下。。。果然有规律hhhhh
需要注意的是要分奇偶。。。
如果是偶数可能会多算。。。因为这个wa了一发。。。


/*************************************************************************
> File Name: code/zoj/3629.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年10月24日 星期六 20时07分02秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>

#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
using namespace std;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
LL a,b;
void pre()
{
int sum =  1;
for ( int i =1; i <= 300 ; i++)
{
int res = 0 ;
for ( int j = 1 ; j <= i ; j++)
res = res + i/j;
if (res%2==0)
{
sum++;
cout<<"i:"<<i<<endl;
}

}

printf(" sum: %d\n",sum);

}

LL cal( LL n) //计算0到n有多少个数满足
{
if(n==0) return 1;
if (n<0) return 0;

LL res = 0 ;
LL sn = sqrt(n);
LL k = sn/2 + 1; //k为等差数列的项数,第k项为4k-3

res = (2*k-1)*k;
if (sn%2==0)
{
res = res -(4*k-3);
res = res+n-(2*k-2)*(2*k-2)+1;
}

//  cout<<"k:"<<k<<" n:"<<n<<" res:"<<res<<endl;
return res;

}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif

//pre();
while (scanf("%lld %lld",&a,&b)!=EOF)
{
printf("%lld\n",cal(b)-cal(a-1));
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: