gym 100947I (求因子)
Alex is a very clever boy, after all he is the son of the greatest watchmaker in Odin.
One day, Alex was playing with some old watches and he found n gears, each gear has ai teeth in it. Alex likes to make beautiful pairs of gears, he thinks a pair of gears is beautiful if we attach the two gears together and spin the first gear exactly one rotation, then the other gear spins an integer number of rotations. For example a pair of 8 and 4 is beautiful, whereas a pair of 8 and 5 isn't, neither is pair of 4and 8.
Now Alex is curious, he wants to know how many beautiful pairs are there. Counting is not Alex's thing, so he asked you to help him.
The first line of input contains one integer T: The number of test cases you need to process.
Each test case consists of two lines. The first line is a single integer n: the number of gears Alex has. The second line contains n space separated integers ai: the number if teeth in the ith gear.
1 ≤ n ≤ 104
2 ≤ ai ≤ 106
For each testcase print a single integer: the number of distinct pairs that satisfy the problem statement.
2
5
4 6 7 8 12
6
2 2 2 3 3 4
3
7
note that we consider two pair distinct when they differ by at least one gear.
In the first sample the pairs are: (4,8) , (4,12) , (6,12)
题意:
如果两个数是倍数关系,那么他们就是beautiful,问有多少对beautiful。
思路:
对于一个数,只求他的每个因子在序列中有几个就可以了,这样就不会重复,注意要先排序,不然有些会计算不到。
代码:
/** @xigua */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include<climits>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 1e8 + 5;
const ll inf = 1e15 + 5;
const db eps = 1e-9;
int vis[maxn];
int a[maxn]; void solve() {
memset(vis, 0,sizeof(vis));
int n; cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
sort(a+1, a+1+n);
for (int i = 1; i <= n; i++) {
int x = a[i];
for (int j = 1; j * j <= x; j++) {
if (x % j == 0) { //是因子
ans += vis[j]; // 看该因子出现过几次
int xx = x / j;
if (xx != j) {
ans += vis[xx]; //另一个不同因子,因为因子是成对出现的
}
}
}
vis[x]++; //先计算后标记
}
cout << ans << endl;
} int main() {
//cin.sync_with_stdio(false);
//freopen("isharp.in", "r", stdin);
//freopen("isharp.out", "w", stdout);
int t = 1; cin >> t;
while (t--) {
solve();
}
return 0;
}
gym 100947I (求因子)的更多相关文章
- Trailing Zeroes (I) LightOJ - 1028(求因子个数)
题意: 给出一个N 求N有多少个别的进制的数有后导零 解析: 对于一个别的进制的数要转化为10进制 (我们暂且只分析二进制就好啦) An * 2^(n-1) + An-1 * 2^(n-2) + `` ...
- POJ1845:Sumdiv(求因子和+逆元+质因子分解)好题
题目链接:http://poj.org/problem?id=1845 定义: 满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元. 为什么要有乘法逆元呢? 当我们要求(a/b) mod p的 ...
- POJ-2992 Divisors---组合数求因子数目
题目链接: https://cn.vjudge.net/problem/POJ-2992 题目大意: 给出组合数Cnk,求出其因子个数,其中n,k不大于431,组合数的值在long long范围内 解 ...
- HDU-1492-The number of divisors(约数) about Humble Numbers -求因子总数+唯一分解定理的变形
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- LightOj1028 - Trailing Zeroes (I)---求因子个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1028 题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它 ...
- POJ 2992 Divisors (求因子个数)
题意:给n和k,求组合C(n,k)的因子个数. 这道题,若一开始先预处理出C[i][j]的大小,再按普通方法枚举2~sqrt(C[i][j])来求解对应的因子个数,会TLE.所以得用别的方法. 在说方 ...
- hdu 6069 Counting Divisors(求因子的个数)
Counting Divisors Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版
题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ...
- Almost All Divisors(求因子个数及思维)
---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Alm ...
随机推荐
- Prime Cryptarithm
链接 分析:对于三位数我们限定为[100,999],两位数我们限定为[10,99],然后我们依次判断是否满足乘法式且各个数位是否在数列中,若都满足+1 /* PROB:crypt1 ID:wangha ...
- Barn Repair
链接 分析:我们不断统计相邻两个元素之间的差值,按照差值从大到小排序,在进行贪心即可 /* PROB:barn1 ID:wanghan LANG:C++ */ #include "iostr ...
- 【前端】CentOS 7 系列教程之二: 安装 git 最新版
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_2.html 这一篇我们来安装git高版本. 卸载yum安装的旧版本 yum remove git 安装 ...
- Struts2 关于返回type="chain"的用法.
1.转自:https://blog.csdn.net/wuye/article/details/73274852 功能与redirect的action转发类似,不过与redirectaction转 ...
- 【202】ThinkPad手势&快捷键
快捷键: Ctrl + Alt + ↑:正常屏幕Ctrl + Alt + ↓:翻转屏幕Ctrl + Alt + →:向左侧翻转90°Ctrl + Alt + ←:向右侧翻转90° 首先看下 Esc 键 ...
- hdoj1176【DP】
DP基础吧.A掉还是挺爽的.就是考虑在两端只能是从前一秒的内部一米或原来的点来进行,但是在5秒以内可到达点是逐渐外扩的,并不是[0,10],所以就特殊考虑了一下.后面两端是0和10,中间的点可以从上一 ...
- hdoj1003【DP】
这道题目的DP,写到现在才明白... 每次加或者不加的条件就是这个前面这个子序列合是不是大于等于0,如果不是加了就会让这个位置的值没有意义,如果大于0,他还是在往递增的方向继续前进. 以及这个条件的继 ...
- bzoj 1176 [Balkan2007]Mokia 【CDQ分治】
W过大,很难在线维护,考虑离线算法 给每个操作加一个时间属性n,显然,对于n=i的询问,对它有影响的修改只在n<i中,所以可以CDQ(因为是按时间序读进来的,所以不用排序了 对于统计矩形和,可以 ...
- P2579 [ZJOI2005]沼泽鳄鱼
传送门 话说邻接矩阵居然还能快速幂的么-- 把原图的邻接矩阵\(G\)打出来,那么\(G[u][v]\)表示一秒后\(u\)到\(v\)的方案数,\(G^2[u][v]\)表示\(2\)秒后的方案数- ...
- spring MVC 文件上传错误
1.The request sent by the client was syntactically incorrect () http://luanxiyuan.iteye.com/blog/187 ...