ZOJ3629 Treasure Hunt IV(找到规律,按公式)
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
。。
。然后非常喜悦。打了100表,发现真的是这个规律。就像哥伦布发现新大陆一样。于是開始默默地推公式。
。。
然后还是wa了。就在看自己公式哪里推错了,,,,实际上是一个地方爆了long long,唉,做题不慎重啊。
。。。
#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(找到规律,按公式)的更多相关文章
- ZOJ3629 Treasure Hunt IV(找规律,推公式)
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- zoj 3629 Treasure Hunt IV 打表找规律
H - Treasure Hunt IV Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- zoj Treasure Hunt IV
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 1066 Treasure Hunt(相交线段&&更改)
Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...
- Treasure Hunt
Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- poj1066 Treasure Hunt【计算几何】
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8192 Accepted: 3376 Des ...
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- 湖南大学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 ...
随机推荐
- linux编程进阶书推荐APUE,UNP
编程进阶这里强烈推荐<unix环境高级编程>(简称APUE)和<unix网络编程>(简称UNP),这两本书可是经典中的经典啊,作 者是大名鼎鼎的 W.Richard Steve ...
- 使用NFS安装oracle软件
昨天.使用openfiler创建nas存储系统,安装oracle软件,在所有正面最好,要创建一个数据库时报ora错,原来使用nfs安装oracle数据库,mount选项有特殊要求,如以下.以备查: R ...
- _00019 Storm架构介绍和Storm获取案例(简单的官方网站Java案例)
博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...
- 彻底理解Javascript原型继承
彻底理解Javascript原型继承 之前写过一篇Javascript继承主题的文章,这篇文章作为一篇读书笔记,分析的不够深入. 本文试图进一步思考,争取彻底理解Javascript继承原理 实例成员 ...
- XAML基础(一)
1.0 XAML是啥? XAML(eXtensible Application Markup Language,可 扩展应用 程序标记语言) 是一种声明性的XML语法 ,像WPF,WF或者Silver ...
- ECSHOP分类页面筛选功能(按分类下子分类和品牌筛选)
其实分类页面里面本来就有相关的品牌.属性.分类的筛选功能在category.php和模板加上相应的功能即可 1.读出当前分类的所有下级分类 $chlidren_category = $GLOBALS[ ...
- Spring Security 3.2.x与Spring 4.0.x的Maven依赖管理
原文链接: Spring Security with Maven原文日期: 2013年04月24日翻译日期: 2014年06月29日翻译人员: 铁锚 1. 概述 本文通过实例为您介绍怎样使用 Mave ...
- 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]
Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...
- mysql替换字段里数据内容部分字符串(亦可用于增加字段中的内容)
mysql替换表的字段里面内容,如例子: mysql> select host,user from user where user='testuser'; +----------------- ...
- windows phone 加速计(5)
原文:windows phone 加速计(5) 在windows phone 中存在着加速计,我们可以利用加速计获得用户手机的状态,根据手机状态调整我们的程序,这样会更人性化:windows phon ...