Treasure Hunt IV


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 from b 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

Author: QU, Zhe

Contest: ZOJ Monthly, July 2012



题目大意:给你一个区间,问你这个区间内的特殊数有多少,特殊数n的定义是n/1+n/2+n/3+....+n/n为偶数就可以。

    解题思路:不要左想右想,直接打表看奇数偶数的特性,開始打了20,发现是1个0。3个1。5个0,7个1。

。。

。然后非常喜悦。打了100表,发现真的是这个规律。就像哥伦布发现新大陆一样。于是開始默默地推公式。

。。


发现不是非常好推。我把这个结论告诉了yb,以为他有简单的方法解决一下,最后我还是自己拿了纸和笔到旁边推公式去了。

能够找规律。区间落点各自是1 2^2 3^2 4^2 5 ^2
1.直接开根号。看他们在哪个区间。
2.假设他们在一个区间。直接求和就可以。
3.看他们落在奇数点还是偶数点。然后分情况讨论。
4.推0个数的求和个数公式。

好吧,我又坑了。
第一发wa是由于从0開始计数的,而我是从1開始计数的,所以a,b都要加1。再计算。

然后还是wa了。就在看自己公式哪里推错了,,,,实际上是一个地方爆了long long,唉,做题不慎重啊。

。。。


题目地址:Treasure Hunt IV


AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std; int main()
{
unsigned long long a,b;
unsigned long long p1,p2; //printf("%lld\n",(1<<63)-1); unsigned long long ans,t1,t2;
while(cin>>a>>b)
{
a++,b++;
p1=sqrt(a+0.5);
p2=sqrt(b+0.5); if(p1==p2) //假设他们落到了同一个地方
{
if(p1&1)
{
if(p1*p1==a)
printf("1\n");
else
printf("0\n");
}
else
{
if(p1*p1==a)
cout<<b-a<<endl;
else
cout<<b-a+1<<endl;
}
continue;
} ans=0;
if(p1&1) //ok
{
if(p1*p1==a)
ans+=1;
t1=p1+2;
}
else
{
if(p1*p1==a)
t1=p1+1;
else
{
ans+=(p1+1)*(p1+1)-a+1; //就是这个地方爆的long long啊,巨坑。 。
t1=p1+3;
}
} if(p2&1)
{
t2=p2;
}
else
{
ans+=b-p2*p2;
t2=p2-1;
} t1=(t1+1)/2,t2=(t2+1)/2,t1--; //以下是用公式计算中间的0,个数为1,5,9,13,
//通项为4n-3,求和公式为(2n-1)*n
if(t2>=t1)
ans=ans+(2*t2-1)*t2-(2*t1-1)*t1; cout<<ans<<endl;
}
return 0;
} /*
0 11538571374624767
5769285672726615
123 321
87
*/



版权声明:本文博客原创文章,博客,未经同意,不得转载。

ZOJ3629 Treasure Hunt IV(找到规律,按公式)的更多相关文章

  1. ZOJ3629 Treasure Hunt IV(找规律,推公式)

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

  2. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  3. zoj Treasure Hunt IV

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

  4. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  5. POJ 1066 Treasure Hunt(相交线段&amp;&amp;更改)

    Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...

  6. Treasure Hunt

    Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. poj1066 Treasure Hunt【计算几何】

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8192   Accepted: 3376 Des ...

  8. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  9. 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1

    题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...

随机推荐

  1. SE 2014 年4月21日(一)

    如图配置 网络中存在四个自治系统,设备之间要求建立BGP对等体关系,发布BGP路由,使得全网BGP路由相互通讯. 要求 1. 由于AS 200中的路由信息频繁抖动,所以AS 100 和 AS 300 ...

  2. Java程序猿笔试面试之String1

    1.怎样实现字符串的反转比如:"how are you"--->"you are how" public class InverseString { pu ...

  3. html+css实现登录界面

    <!DOCTYPE html> <style type="text/css"> body{ background-color: #555555; } #ti ...

  4. Java 理论与实践: 处理 InterruptedException(转)

    很多 Java™ 语言方法,例如 Thread.sleep() 和 Object.wait(),都可以抛出InterruptedException.您不能忽略这个异常,因为它是一个检查异常(check ...

  5. SQL Server :理解数据记录结构

    原文:SQL Server :理解数据记录结构 在SQL Server :理解数据页结构我们提到每条记录都有7 bytes的系统行开销,那这个7 bytes行开销到底是一个什么样的结构,我们一起来看下 ...

  6. Windows Phone开发(3):棋子未动,先观全局

    原文:Windows Phone开发(3):棋子未动,先观全局 在进行WP开发之前,与其它开发技术一样,我们需要简单了解一个WP应用序的生命周期,我们不一定要深入了解,但至少要知道在应用程序生命周期内 ...

  7. accept功能

    accept()功能 系统调用 accept() 这将是一个有点陌生的地方! 你可以想象发生 这种事情:这是非常远离你通过倾听 (listen()) 的port连接 (connect()) 你的机器. ...

  8. 第2周 页_SQL Server 中数据存储的基本单位

    原文:第2周 页_SQL Server 中数据存储的基本单位 上周通过探讨SQL Server如何执行一个查询奠定了基础.我也在那里提到页是8kb的缓存.今天我们对页进行进一步集中探讨,从性能调优角度 ...

  9. android 4.0 中出错 java.lang.UnsupportedOperationException

    在android4.0中  画图的时候使用: canvas.clipPath(path, Region.Op.XOR); 报错 java.lang.UnsupportedOperationExcept ...

  10. Visual Studio 2010 将网站直接发布到远程站点

    原文:Visual Studio 2010 将网站直接发布到远程站点 这次说下如何将web应用程序直接发布到IIS服务器站点!!! 问题的由来 本人每天要发布更新的程序,所以每次更新的时候要做的工作: ...