Satisfactory Pairs

来源:互联网 发布:阿里云备案.top 编辑:程序博客网 时间:2024/06/03 17:17

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;}


原创粉丝点击