D - Satisfactory Pairs HackerRank - pairs-again

Given a positive integer, , find and print the number of pairs of positive integers , where , that exist such that the equation (where and are positive integers) has at least one solution.

Input Format

A single positive integer denoting .

Constraints

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 pairs: and .

step1: 读题:计算有多少对(a, b)(a < b)使得ax+by=n存在至少一组正整数解

step2:算法:预处理前n个数的约数(类似欧拉筛法), 然后暴力

step3:套用算法:处理完约数之后暴力,先暴力枚举a然后ax,从而计算可得yb,然后枚举所有yb的约数其中b>a的就ans++;

错误点:去重的时候并没有考虑到ab的一一对应关系,而是单独的使用了bool数组标记了0或1导致错误,正确标记方法应为flag[b] = a; 这种情况是为了避免ax+by=n中xy有多组解,但ab是固定的

step4:AC代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 310005
#define met(a, b) memset(a, b, sizeof(a))
vector<int> DIV[maxn];
int flag[maxn];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 2; i < n; i++)
    {
        for(int j = i; j < n; j+= i)
        {
            DIV[j].push_back(i);
        }
    }
    int ans = 0;

    met(flag, 0);
    for(int a = 1; a < n; a++)
    {
        for(int xa = a; xa < n; xa += a)
        {
            int yb = n - xa;
            for(int i = 0; i < DIV[yb].size(); i++)
            {
                int v = DIV[yb][i];
                if(flag[v] == a || v <= a) continue;
                ans++;
                flag[v] = a;
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值