HDU-4729 Number

来源:互联网 发布:js 跳转url target 编辑:程序博客网 时间:2024/06/07 14:26

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
21 11 10
Sample Output
04          
Hint
 For the second case, the real numbers are 6,8,9,10. 

打表找规律,可以发现先不管平方数,从6开始的偶数全部都是,然后再加上奇数的完全平方数。如果只看从6开始的偶数,那就是(n-4)/2,然后再算平方数,当前数开根是个奇数说明需要加1,否则不加(跟前面的奇数平方数相抵消)。

#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5+7;const long long mod = 1e9+7;long long n,m;long long cal(long long p){    if(p < 6)return 0;    long long t = sqrt((long double)p);    return (p-4)/2 + t%2;}int main(){    int t;    scanf("%d",&t);    while(t--)    {   scanf("%I64d%I64d",&n,&m);        printf("%I64d\n",cal(m) - cal(n-1));    }    return 0;}



原创粉丝点击