洛谷 - T42830 - Oier们的幸运数字(数论)

版权声明:Andy https://blog.csdn.net/Alibaba_lhl/article/details/82874009

 

Sample Input  

3
1 10
11 15
5 12

Sample Output 

No
No
Yes

Source::Click here

思路

因为只有 奇数+偶数=奇数。所以只有当[a,b]内的f(i) 是奇数的个数为奇数个时,才满足求和为奇数。经规律发现:f(i)为奇数时,i 的集合为{1^2,3^2,5^2,7^2......},即满足sqrt(i)是奇数。所以需要对所给定的区间[a,b]内的元素进行判断,所以需对左端点的开方值向上取整,记为x,对右端点的开方值向下取整,记为y。所以接下来就是统计[x,y]区间内为奇数的个数。

                                                                                                                                                                                            

AC Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = (int)1e6+10;

int main()
{
    int T;
    ll a, b;
    scanf("%d",&T);
    while(T--)
    {
        ll sum;
        scanf("%lld %lld",&a,&b);
        ll x = ceil(sqrt(a)), y = sqrt(b);
        if(a==b)///特判
        {
            if(x*x==a && x&1) printf("Yes\n");
            else printf("No\n");
            continue;
        }
        if(!(x&1)) x++;///x为偶数数时,对结果不做贡献
        if( ((y-x)/2+1) & 1) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Alibaba_lhl/article/details/82874009