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;
}
本文介绍了一种算法,该算法用于找出所有符合条件的正整数对(a, b),使得方程x*a + y*b = n至少有一个正整数解(x, y)。通过预处理得到n以内所有数的因数,并使用暴力枚举的方法来寻找满足条件的整数对。
9176

被折叠的 条评论
为什么被折叠?



