Dertouzos (結論賽高)

A positive proper divisor is a positive divisor of a number nn, excluding nn itself. For example, 1, 2, and 3 are positive proper divisors of 6, but 6 itself is not. 

Peter has two positive integers nn and dd. He would like to know the number of integers below nn whose maximum positive proper divisor is dd.
InputThere are multiple test cases. The first line of input contains an integer TT (1T106)(1≤T≤106), indicating the number of test cases. For each test case: 

The first line contains two integers nn and dd (2n,d109)(2≤n,d≤109). OutputFor each test case, output an integer denoting the answer. 
Sample Input
9
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
100 13
Sample Output
1
2
1
0
0
0
0
0

4

剛開始不知道結論,就直接暴力求解,當然超時了LOL

  1. 設p爲d的最小質因子,a爲2到p的所有質數,以d爲最大非平凡(真)因子的數爲a*d。 

證明:


ad=n,d的最小的質因子爲p,d=pp,p最小爲一

假設a>p

n=app=p(ap)

a>p

ap>pp=d

a>pdn

dna<=p

dnan

至此結論得證

所以:

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string.h>
#define LL long long
using namespace std;
bool isprime[100010];
int prime[100010],cnt;
void init(){
    memset(isprime,1,sizeof isprime);
    isprime[0]=isprime[1]=false;
    cnt=0;
    for(int i=2;i<=100000;++i){
        if(isprime[i]){
            prime[cnt++]=i;
            for(LL j=(LL)i*i;j<=100000;j+=i)
                isprime[j]=false;
        }
    }
}
int main(){
    int t;
    init();
    scanf("%d",&t);
    while(t--){
        int n,d;
        int ans=0;
        scanf("%d%d",&n,&d);
        n=(n-1)/d;
        for(int i=0;i<cnt&&prime[i]<=n;++i){
            ++ans;
            if(d%prime[i]==0)
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章