首页 > 代码库 > HDU 4279 Number(找规律)
HDU 4279 Number(找规律)
Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4288 Accepted Submission(s): 1066
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.
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 4HintFor the second case, the real numbers are 6,8,9,10.
Source
field=problem&key=2012+ACM%2FICPC+Asia+Regional+Tianjin+Online&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">2012 ACM/ICPC Asia Regional Tianjin Online
题目就是求这个F(x)
题解:打个表找规律,F(x)=x/2-1+(sqrt(x)%2? 0:-1) 推荐证明:http://blog.csdn.net/acdreamers/article/details/9730423
注:sqrt()存在精度问题。精度处理不好会wa.
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #define ll long long using namespace std; ll Sqrt(ll l,ll r,ll a) { ll mid=(l+r)/2; if(l>r) return r; if(a/mid>mid) return Sqrt(mid+1,r,a); else if(a/mid<mid) return Sqrt(l,mid-1,a); else return mid; } ll solve(ll r) { if(r<6)return 0; return r/2-2+(ll)Sqrt(1,r,r)%2;; } int main() { int t; cin>>t; while(t--) { ll l,r; scanf("%I64d%I64d",&l,&r); printf("%I64d\n",solve(r)-solve(l-1)); } return 0; }
HDU 4279 Number(找规律)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。