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
Sample Output 0
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;
}
, 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;
}
相关文章推荐
- D - Satisfactory Pairs HackerRank - pairs-again(查找因数组合题目)
- Satisfactory Pairs HackerRank - pairs-again
- Satisfactory Pairs HackerRank - pairs-again——预处理+不定长数组应用
- 【数学思维 && ax + by = n 给你n让你求有多少对(a,b) a<b 满足方程其中x,y是正整数】HackerRank Satisfactory Pairs
- Satisfactory Pairs (HackerRank pairs-again)
- [HackerRank-pairs-again] Satisfactory Pairs
- HackerRank - pairs-again(暴力+预处理)
- 【HackerRank】Pairs
- HackerRank Algorithms-Search-Pairs 数组的固定差对数
- HackerRank "Tower Breakers, Again!"
- HackerRank "Chessboard Game, Again!"
- 【Hacker Rank】02.Reading Raw Input
- Python HackerRank|The Captain's Room
- Python HackerRank|DefaultDict Tutorial(from collections import defaultdict)
- Codewars, Leetcode, Hackerrank. Online Judges Reviews
- HackerRank Palindromes
- hackerrank>Dashboard>C++>STL>Print Pretty
- Hackerrank Equal(这特么也是dp???)
- hackerrank Piling Up!
- HackerRank Between Two Sets