Given a positive integer n, find and print the number of pairs of positive integers(a,b), where(a < b) , that exist such that the equation x * a + y * b = n (where x and yare positive integers) has at least one solution.
Input Format
A single positive integer denoting n.
Constraints
- 4 <= n <= 3 * 10 ^ 5;
Output Format
Print a single integer denoting the number of such pairs.
Sample Input 0
4
Sample Output 0
2
Explanation 0
There are two such (a, b) pairs: (1,2) and (1, 3).
預處理N以內所有數的約數,暴力枚舉。
#include <bits/stdc++.h>
using namespace std;
vector<int>::iterator it;
vector<int>str[500010];
int n, used[500010], ans;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j * j <= i; j++)
{
if(i % j == 0)
{
str[i].push_back(j);
if(j * j != i)
str[i].push_back(i / j);
}
}
sort(str[i].begin(), str[i].end(), cmp);
}
ans = 0;
for(int a = 1; a < n; a++)
{
for(int x = 1; x * a < n; x++)
{
int yb = n - a * x;
for(it = str[yb].begin(); it != str[yb].end(); it++)
{
if((*it) <= a)
break;
if(used[*it] != a)
{
ans++;
used[*it] = a;
}
}
}
}
printf("%d\n", ans);
return 0;
}