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. HDU 2086 A1 = ? (找规律推导公式 + 水题)(Java版)

    Equations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2086 ——每天在线,欢迎留言谈论. 题目大意: 有如下方程:Ai = (Ai-1 ...

  5. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  6. HDU 1165 Eddy's research II(给出递归公式,然后找规律)

    - Eddy's research II Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  7. BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...

  8. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  9. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

随机推荐

  1. 语法错误1:TabError: Inconsistent use of tabs and spaces in indentation

    如图错误: 出错原因: 由于写代码过程用的tab缩进 解决方法: 把tab缩进改用空格缩进

  2. BZOJ 3831 单调队列DP

    思路: 这好像是我刚学单调性的时候做的题 (我是不会告诉你 我被这题教做人了的...) i-stk[head]>k 删队头 f[stk[tail]]>f[i]||(f[stk[tail]] ...

  3. CORS 和 JSONP

    跨域资源共享(CORS) 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制. CORS(Cross-Origin Resource Sharing) ...

  4. 精确获取对象的类型:Object.prototype.toString()

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

  5. hibernate_08_关联映射_一对多

    hibernate的映射关系 一对多.多对一.一对一.多对多. 常用的是一对多和多对一. 在数据库中可以通过添加主外键的关联,表现一对多的关系:在hibernate中通过在一方持有多方的集合实现,即在 ...

  6. dotnetnuke7.x 弹出窗口的皮肤加载问题

    皮肤文件夹中必须要有popUpSkin.ascx才会正常加载skin.css文件

  7. veket智能机器人

    官方网站:http://www.lucky8k.com/forum.php veket儿童操作系统 希望工具超过小puppy linux系统 一个还在发展中的linux系统: Slax Linux : ...

  8. ubuntu操作系统的目录结构

    /:根目录,是所有目录的绝对路径的起始点.一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin (类似的还有/usr/bin) ...

  9. 自定义View实现拖动小圆球,并随机改变其颜色

    //简单实现package com.example.demo1; import android.content.Context;import android.graphics.Canvas;impor ...

  10. Django MVC与MTV概念 Ajax、分页实现

     MVC与MTV概念 MTV与MVC(了解)        MTV模型(django):            M:模型层(models.py)            T:templates      ...