【CSU 1756】Prime
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的更多相关文章
- 一本通1619【例 1】Prime Distance
1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...
- 【POJ 2689】 Prime Distance
[题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...
- 【CSU 1079】树上的查询
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1079 现有一棵有N个顶点的树,顶点的标号分别为1, 2, …, N.对于每个形如a b k的询问, ...
- 【CSU 1803】2016
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 Solution: 考虑两个数x,y乘积%2016=0 x×y≡0(MOD 2016) x= ...
- 【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)
Prime Numbers Descriptions: A prime number is a natural number which has exactly two distinct natur ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- 【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 ...
- 【Codeforces 912E】Prime Gift
Codeforces 912 E 题意:给\(n\leq16\)个素数\(p_1..p_n\),求第\(k\)个所有质因数都在\(n\)个数中的数. 思路:折半搜索...我原来胡搞毛搞怎么也搞不动\( ...
- 【CSU 1803】2016 (数学)
Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 ...
随机推荐
- mui.openWindow的html5+和web传参的兼容
mui.openWindow兼容web&plus环境下的页面传参 背景介绍 刚刚好要写个微信公众号和html5+兼容的项目 发现总是用localStorage传参不是事啊 太不优雅了 想了想还 ...
- 学习Python一年,基础忘记了,看看面试题回忆回议,Python面试题No3
这边有几个面试题,好棒 第1题:你如何管理不同版本的代码? git,svn两个都要说到,github,码云也要提及,面试官想要的就是版本管理工具,你只要选择一个你熟悉的,疯狂的说一通就可以了,最好说一 ...
- python 去掉html中其他属性,只保留href 和 src
https://segmentfault.com/q/1010000010845573 import re #reg=r'\s+[^(href)]*=\"[^<>]+\" ...
- Qt笔记——添加菜单图片/添加资源文件
添加新文件,模板选择Qt分类中的Qt资源文件(Qt Resource File) 先添加前缀,点击"添加"按钮,然后选择"添加前缀",我们这里因为要添加图片,所 ...
- CodeForces 159E
题目大意: 给定一堆带颜色和高度的魔方 用两种颜色的魔方,一种颜色接一种颜色向上拼接搭建成一个高塔,求高塔的最长高度,以及将拼接的过程中对应的编号顺序输出 多种情况成立输出任意一种即可 这里首先要对颜 ...
- codeforces 369B
#include<stdio.h>//题没读懂,没做出来 int main() { int n,k,l,r,s,s1,m,a,i; while(scanf("%d%d% ...
- 【BZOJ3669】魔法森林(LCT)
题意:有一张无向图,每条边有两个权值.求选取一些边使1和n连通,且max(a[i])+max(b[i])最小 2<=n<=50,000 0<=m<=100,000 1<= ...
- ubuntu14.04 配置网络
ubuntu14.04 配置网络的练习 本文参考的资料: https://blog.csdn.net/liu782726344/article/details/52912797. 感谢作者的分享! 打 ...
- C#高级编程第9版 第一章 .NET体系结构 读后笔记
.NET的CLR把源代码编译为IL,然后又把IL编译为平台专用代码. IL总是即时编译的,这一点的理解上虽然明白.当用户操作C#开发的软件时,应该是操作已经编译好的程序.那么此时安装在客户机上的程序是 ...
- FZU Problem 2171 防守阵地 II (线段树区间更新模板题)
http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...