博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4279 Number 第37届ACM/ICPC天津赛区网络赛1002题 (简单规律题)
阅读量:7021 次
发布时间:2019-06-28

本文共 1907 字,大约阅读时间需要 6 分钟。

Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 640    Accepted Submission(s): 207

Problem Description
  Here are two numbers A and B (0 < A <= B). If B cannot be divisible by A, and A and B are not co-prime numbers, we define A as a special number of B.
  For each x, f(x) equals to the amount of x’s special numbers.
  For example, f(6)=1, because 6 only have one special number which is 4. And f(12)=3, its special numbers are 8,9,10.
  When f(x) is odd, we consider x as a real number.
  Now given 2 integers x and y, your job is to calculate how many real numbers are between them.
 

 

Input
  In the first line there is an integer T (T <= 2000), indicates the number of test cases. Then T line follows, each line contains two integers x and y (1 <= x <= y <= 2^63-1) separated by a single space.
 

 

Output
  Output the total number of real numbers.
 

 

Sample Input
2 1 1 1 10
 

 

Sample Output
0 4
Hint
For the second case, the real numbers are 6,8,9,10.
 

 

Source
 

 

Recommend
liuyiding
 
 
 
这题就是找规律。这样的题目比赛时没有快速做出来。。。只能对自己无语了。。。
 
很容易发现 所谓的 real number  就是大于4而且不是偶数的平方的偶数,或者是奇数的平方的奇数。
 
所以可以写个函数求出小于等于n的real number的个数。
 
n小于等于4,real number的个数一定为0.
(n-4)/2表示大于4的偶数的个数。但这其中包括了偶数的平方,少了奇数的平方。
所以如果 k*k<=n<(k+1)*(k+1).假如k是偶数,那么就是 (n-4)/2 ,因为偶数的平方和奇数的平方个数相等。
假如k是奇数,那么就是  (n-4)/2+1了,因为奇数的平方多一个。
 
其余就简单了。注意数据范围,用 long long
 
#include
#include
#include
#include
#include
using namespace std;//大于4,而且不是偶数的平方数的偶数是real number//奇数的平方的奇数是real numberlong long calc(long long n)//计算小于等于n的real number的个数{ if(n<=4)return 0; long long t=sqrt(n); long long ans=(n-4)/2;//大于4的偶数的个数 if(t%2==0)return ans; else return ans+1;}int main(){ int T; long long A,B; scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&A,&B); printf("%I64d\n",calc(B)-calc(A-1)); } return 0;}

 

转载地址:http://lbbxl.baihongyu.com/

你可能感兴趣的文章
sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)
查看>>
epoll 与 java nio调优
查看>>
objective-c基础教程——学习小结
查看>>
linux解压命令
查看>>
关于dd/bs和swap/swappiness
查看>>
【推荐】The Function Pointer Tutorials
查看>>
51CTO博客开通了!
查看>>
Google Reader或死于高昂的隐私诉讼案
查看>>
缓冲区溢出(二)
查看>>
js高级程序设计笔记(13章)
查看>>
Nginx安装PHP-FPM及优化
查看>>
Zabbix 3.2 监控部署
查看>>
seekBar,RatingBar拖动条
查看>>
JavaScript原型、原型链和继承
查看>>
git错误集
查看>>
谷歌深度学习公开课任务 1: notMNIST
查看>>
java锁的理解
查看>>
本地连接已连接上,但电脑无法上网
查看>>
WinAPI: PolyPolygon - 绘制一组多边形
查看>>
MusicXML 3.0 (16) - 小音符
查看>>