【Kickstart】2019 Round E - Street Checkers

解法

就是要求[L,R]里的每个整数中,有多少个整数的奇因子和偶因子个数之差不超过2.

  • 对于奇数来说,它只可能有奇因子,那么需要奇因子个数少于2,那么只能是质数或1
  • 对于偶数来说,假设x=2mnx = 2^m\cdot n,其中n是奇数,那么x的因子有这几种情况:
    1. 奇x偶: 那么应该是(a,b2m)(a,b\cdot 2^m),其中ab=n,这时候不管ab相不相等,这都是2个不同的因子,一奇一偶,抵消,所以奇因子个数与偶因子个数之差为0
    2. 偶x偶: 所以这个时候要求这部分的因子个数不能超过2,否则偶就比奇多了。这时候应该有(a2i,b2mi)(a\cdot2^i,b\cdot2^{m-i}),其中ab=ni>0
      • 0个因子:m不能拆分,也就是m==1
      • 1个因子:a2i=b2mia\cdot2^i=b\cdot2^{m-i},要求n==1m==2
      • 2个因子:【n为质数,且m==2】或者【n=1m==3
#include <stdio.h>
#include <string>
#include <iostream>
#include <memory.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <functional>

#define MAXN 100010
#define NINF -100000
#define INF 65536


using namespace std;

typedef long long lld;

int L,R;

bool isZhiShu(int x) {
    if (x==2||x==3) return true;
    if (x%6!=1 && x%6!=5) return false;
    int up = (int)floor(sqrt(x));
    for (int d =3;d<=up;d += 2) {
        if (x%d==0) return false;
    }
    return true;
}

bool able(int x) {
    if (x==1) return true;
    if (x%2!=0) return isZhiShu(x);
    int even = 0;
    while (x>0 and x%2==0) {
        even++;
        x = x>>1;
    }
    return even == 1 || (even == 3 && x==1) || (even == 2 && isZhiShu(x));
}

int solve() {
    int ans = 0;
    for(int i=L;i<=R;++i) {
//        cout<<i<<" "<<able(i)<<endl;
        if (able(i)) ans++;
    }
    return ans;
}


int main() {
//    freopen("/Users/huangyuemei/CLionProjects/untitled/C-small-practice.in","r",stdin);
//    freopen("/Users/huangyuemei/CLionProjects/untitled/my.out","w",stdout);
    int t;
    scanf("%d",&t);
    for(auto round=1;round<=t;++round) {
        scanf("%d%d",&L,&R);
        printf("Case #%d: %d\n",round,solve());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章