Dertouzos
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2054 Accepted Submission(s): 620
Problem Description
A positive proper divisor is a positive divisor of a number n,
excluding n itself.
For example, 1, 2, and 3 are positive proper divisors of 6, but 6 itself is not.
Peter has two positive integers n and d. He would like to know the number of integers below n whose maximum positive proper divisor is d.
Peter has two positive integers n and d. He would like to know the number of integers below n whose maximum positive proper divisor is d.
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤106),
indicating the number of test cases. For each test case:
The first line contains two integers n and d (2≤n,d≤109).
The first line contains two integers n and d (2≤n,d≤109).
Output
For 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
Source
題解
題解:給你 t 個 n 和 d ,讓你求小於n的數中最大約數(不包括本身)爲d的數量。
因爲要使最大因數爲d,必存在x*d=m,同時x必須爲質數,且x必須小於d的最小質因數,因此找n前面有幾個m成立即可。因爲 t 範圍很大,所以可以先打表篩選出質數表再線性掃一遍就可以了。
之前一直超時,後來才明白判斷素數時超時
錯誤代碼
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<stdio.h>
#include<math.h>
using namespace std;
const int MAXN=1000010;
int prime[MAXN+1];
int getPrime(int m)
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=m;i++)
{
if(!prime[i])
prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&prime[j]<=m/i;j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)
break;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,ans;
scanf("%d%d",&n,&m);
ans=n/m;
if(ans>m)
{
getPrime(m);
printf("%d\n",prime[0]);
}
else
{
getPrime(ans);
printf("%d\n",prime[0]);
}
}
return 0;
}
AC代碼#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int isprime[100005]={0};
int prime[10005]={0};
int k=0;
int init()
{
int i,j;
for(i=2;i<100000;i++)
{
if(isprime[i]==0)
{
for(j=2;i*j<100000;j++)
{
isprime[i*j]=1;
}
prime[k]=i;//存放素數的表,
//這k表示i(包括i)前面有幾個素數
k++;
}
}
return 0;
}
int main()
{
int t,i;
int n,m;
scanf("%d",&t);
init();
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<k;i++)
{
if(m*prime[i]>=n)
break;
if(m<prime[i])
break;
if(m%prime[i]==0)//想想爲什麼
break;
}
if(m*prime[i]>=n||m<prime[i])
i--;
printf("%d\n",i+1);
}
}