您的位置:首页 > 大数据 > 人工智能

D - Satisfactory Pairs HackerRank - pairs-again

2018-01-25 09:18 274 查看
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 <
4000
;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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: