【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());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章