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 ...
随机推荐
- (转)Vim自动补全神器:YouCompleteMe
原文出处:http://blog.jobbole.com/58978/ 第一次听说这个插件还是在偶然的情况下看到别人的博客,听说了这个插件的大名.本来打算在实训期间来完成安装的,无奈网实在不给力,也就 ...
- IOS中的沙盒机制
IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...
- css 选择器中的正则表达式
正则表达式在任何语言中都有使用,只是使用的形式不一样而已 css也是一门语言,也有自己的正则表达式 正则表达式中的一些通用规则: 1 ^ 表示字符串开始位置匹配 2 $表示字符串结束为止匹配 3 *表 ...
- scrollerView 滚动的时候改变 scrollerView 的背景色代码
要实现点击电池条的时候立即回到页面的顶部的时候注意: 只有当一个主控制器有一个scrollview 并把这个属性设置为yes,其他的scrollview.scrollsToTop = NO 这样才会响 ...
- C# 利用Aspose.Words .dll将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)
下载Aspose.Words .dll http://pan.baidu.com/s/1c8659k 在vs2010中新建窗体应用程序,命名为 wordtopdf 添加Aspose.Words .d ...
- 4种java复制文件的方式
尽管Java提供了一个可以处理文件的IO操作类,但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...
- NYOJ6——喷水装置(一)
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它 ...
- cardboard sdk for unity 系统分析 - 属性行为分析CardBoard类
一.CardBoard类是个单例类,全局只有一个CardBoard对像且在需要时才生成: CardBoard中主要使用了BaseVRDevice device这个对像,而这个对像也是单例类BaseVR ...
- (水题)洛谷 - P1022 - 计算器的改良
https://www.luogu.org/problemnew/show/P1022 服了,居然还有+0.000和-0.000的不同,真的服了. #include<bits/stdc++.h& ...
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...