http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1756

直接暴力O(n^2logn)过不了 两两算gcd

考虑每个数的范围[1,1000]统计一下即可O(1000^2*log(1000))

Notice:1与任何数互质,需要特判(自己与自己互质)

Code1 统计

// <1756.cpp> - Wed Oct 19 08:25:53 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
int a[MAXN];
int main()
{
int T=gi();
while(T--){
int n=gi(),ans=;
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
a[gi()]++;
for(int i=;i<MAXN;i++)
if(a[i])
for(int j=i+;j<MAXN;j++)
if(__gcd(i,j)==)ans+=a[i]*a[j];
ans+=a[]>?(a[]-)*a[]/:;//this
printf("%d\n",ans);
}
return ;
}

Code2 vector 本以为记录一下哪些数出现过(结果发现数据水,还跑得慢一点)

// <1756.cpp> - Wed Oct 19 08:25:53 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
int a[MAXN];vector<int>b;
inline int gcd(register int a,register int b){
return b==?a:gcd(b,a%b);
}
int main()
{
freopen("1756.in","r",stdin);
freopen("1756.out","w",stdout);
int T=gi();
while(T--){
int n=gi(),ans=,to;
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
a[gi()]++;b.clear();
for(int i=;i<MAXN;i++)
if(a[i])b.push_back(i);to=b.size();
for(int i,o=;i=b[o],o<to;o++)
if(a[i])
for(int j,k=o+;j=b[k],k<to;k++)
if(gcd(i,j)==)ans+=a[i]*a[j];
if(b[]==)ans+=a[]>?(a[]-)*a[]/:;//this
printf("%d\n",ans);
}
return ;
}

【CSU 1756】Prime的更多相关文章

  1. 一本通1619【例 1】Prime Distance

    1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...

  2. 【POJ 2689】 Prime Distance

    [题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...

  3. 【CSU 1079】树上的查询

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1079 现有一棵有N个顶点的树,顶点的标号分别为1, 2, …, N.对于每个形如a b k的询问, ...

  4. 【CSU 1803】2016

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 Solution: 考虑两个数x,y乘积%2016=0 x×y≡0(MOD 2016) x= ...

  5. 【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)

    Prime Numbers  Descriptions: A prime number is a natural number which has exactly two distinct natur ...

  6. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  7. 【CSU 1556】Pseudoprime numbers

    题 Description Jerry is caught by Tom. He was penned up in one room with a door, which only can be op ...

  8. 【Codeforces 912E】Prime Gift

    Codeforces 912 E 题意:给\(n\leq16\)个素数\(p_1..p_n\),求第\(k\)个所有质因数都在\(n\)个数中的数. 思路:折半搜索...我原来胡搞毛搞怎么也搞不动\( ...

  9. 【CSU 1803】2016 (数学)

    Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 ...

随机推荐

  1. mysql通配符进行模糊查询

    在mysql数据库中,当我们需要模糊查询的时候 ,我们会使用到通配符. 首先我们来了解一下2个概念,一个是操作符,一个是通配符. 操作符 like就是SQL语句中的操作符,它的作用是指示在SQL语句后 ...

  2. LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Mysql篇

    文章来源:http://www.cnblogs.com/hello-tl/p/7569097.html 更新时间:2017-09-21 16:06 简介 LAMP+R指Linux+Apache+Mys ...

  3. Python中的数据类型之字符串

    字符串的定义,可是使用类似下面的方式 name = "hello python" Python 中字符串自带了一些常用的方法,比如: title() #用来将每个单词首字母大写up ...

  4. Python 中的变量还能这样理解(白话)

    一.案例分析 1.思考 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 坐标:深圳 # 2018年1月份 # 房租水电 4000元 # 伙食费 1000 ...

  5. Django——分页功能Paginator

    Django分页功能----Paginator Paginator所需参数: Paginator(object_list,per_page) Paginator常用属性: per_page: 每页显示 ...

  6. python接口自动化测试(一)

    本节开始,开始介绍python的接口自动化测试,首先需要搭建python开发环境,到https://www.python.org/下载python 版本直接安装就以了,建议 下载python2.7.1 ...

  7. POJ3352-Road Construction(边连通分量)

    It's almost summer time, and that means that it's almost summer construction time! This year, the go ...

  8. Linux下汇编语言学习笔记55 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. linux 命令练习 2018-08-27

    linux 命令练习 2018-08-27 uname  显示系统名字 [test@localhost ~]$ uname Linux uname -a   即列出linux的内核版本号 [test@ ...

  10. hdu_2082_找单词_201404271536

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...