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.
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.
The first line contains two integers nn and dd (2≤n,d≤109)(2≤n,d≤109).
9 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 100 13
1 2 1 0 0 0 0 04
刚开始不知道结论,就直接暴力求解,当然超时了LOL
- 设p为d的最小质因子,a为2到p的所有质数,以d为最大非平凡(真)因子的数为a*d。
所以:证明:
设a∗d=n,d的最小的质因子为p,d=p∗p′,p′最小为一假设a>p
n=a∗p∗p′=p∗(a∗p′)
∵a>p
∴a∗p′>p∗p′=d
∴a>p时,d不是n的最大因子
∴d时n的最大因子时,a<=p
∵d为n的最大因子时,a一定为n的最小因子,而一个数的最小因子一定为质数
至此结论得证
#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; }