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;}
阅读全文
0 0
- Satisfactory Pairs
- Satisfactory Pairs
- Satisfactory Pairs
- Satisfactory Pairs
- Satisfactory Pairs HackerRank
- Satisfactory Pairs HackerRank
- Satisfactory Pairs (HackerRank pairs-again)
- HackerRank - pairs-again Satisfactory Pairs 数论、方程、优化、标记方法、预处理
- pairs
- pairs
- Event Pairs
- Palindrome Pairs
- Palindrome Pairs
- leetCode_Palindrome Pairs
- Palindrome Pairs
- Reverse Pairs
- hdu5178 pairs
- zoj 2520 Amicable Pairs
- 关于类、父类、抽象类、接口之间联系的一点点思考
- Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
- ESP8266使用详解
- html Webpack Plogin
- Linux修改yum源
- Satisfactory Pairs
- ES6遍历属性
- Struts2标签
- 内存对齐
- 数据库原理与应用之数据库标准语言SQL
- AtCoder Regular Contest 076 D Built?(kruskal)
- scipy在pycharm中安装失败解决方法
- 性能调优攻略
- 完全平方