题目链接:HDOJ - 5212

题目分析

首先的思路是,考虑每个数对最终答案的贡献。

那么我们就要求出:对于每个数,以它为 gcd 的数对有多少对。

显然,对于一个数 x ,以它为 gcd 的两个数一定都是 x 的倍数。如果 x 的倍数在数列中有 k 个,那么最多有 k^2 对数的 gcd 是 x 。

同样显然的是,对于两个数,如果他们都是 x 的倍数,那么他们的 gcd 一定也是 x 的倍数。

所以,我们求出 x 的倍数在数列中有 k 个,然后就有 k^2 对数满足两个数都是 x 的倍数,这 k^2 对数的 gcd,要么是 x ,要么是 2x, 3x, 4x...

并且,一个数是 x 的倍数的倍数,它就一定是 x 的倍数。所以以 x 的倍数为 gcd 的数对,一定都包含在这 k^2 对数中。

如果我们从大到小枚举 x ,这样计算 x 的贡献时,x 的多倍数就已经计算完了。我们用 f(x) 表示以 x 为 gcd 的数对个数。

那么 f(x) = k^2 - f(2x) - f(3x) - f(4x) ... f(tx)             (tx <= 10000, k = Cnt[x])

这样枚举每个 x ,然后枚举每个 x 的倍数,复杂度是用调和级数计算的,约为 O(n logn)。

代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue> using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef double LF; inline int gmin(int a, int b) {return a < b ? a : b;}
inline int gmax(int a, int b) {return a > b ? a : b;} inline LF gmin(LF a, LF b) {return a < b ? a : b;}
inline LF gmax(LF a, LF b) {return a > b ? a : b;} const LF Eps = 1e-8; inline LF Sqr(LF x) {return x * x;} inline int Sgn(LF x)
{
if (x < -Eps) return -1;
if (x > Eps) return 1;
return 0;
} const int MaxN = 10000 + 5, Mod = 10007; int n, Ans, Num, Temp, SqrtX;
int Cnt[MaxN], f[MaxN], Pos[MaxN]; int main()
{
while (scanf("%d", &n) != EOF)
{
for (int i = 1; i <= 10000; ++i) Cnt[i] = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &Num);
SqrtX = (int)sqrt((LF)Num);
for (int j = 1; j <= SqrtX; ++j)
{
if (Num % j != 0) continue;
++Cnt[j];
if (Num / j != j) ++Cnt[Num / j];
}
}
Ans = 0;
for (int i = 10000; i >= 1; --i)
{
f[i] = Cnt[i] * Cnt[i] % Mod;
for (int j = i * 2; j <= 10000; j += i)
f[i] = (f[i] - f[j] + Mod) % Mod;
Temp = i * (i - 1) % Mod;
Ans = (Ans + f[i] * Temp % Mod) % Mod;
}
printf("%d\n", Ans);
}
return 0;
}

  

[HDOJ 5212] [BestCoder Round#39] Code 【0.0】的更多相关文章

  1. 暴力+降复杂度 BestCoder Round #39 1002 Mutiple

    题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...

  2. 贪心 BestCoder Round #39 1001 Delete

    题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...

  3. SpringBoot2整合Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the calling code 【已解决】

    SpringBoot集成Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the callin ...

  4. 【CS Round #39 (Div. 2 only) D】Seven-segment Display

    [Link]:https://csacademy.com/contest/round-39/task/seven-segment-display/ [Description] 0..9各自有一个数字, ...

  5. 【CS Round #39 (Div. 2 only) C】Reconstruct Sum

    [Link]:https://csacademy.com/contest/round-39/task/reconstruct-sum/ [Description] 给你一个数字S; 让你找有多少对A, ...

  6. 【CS Round #39 (Div. 2 only) B】Circle Elimination

    [Link]:https://csacademy.com/contest/round-39/task/circle-elimination/ [Description] [Solution] 把n个点 ...

  7. 【CS Round #39 (Div. 2 only) A】Removed Pages

    [Link]: [Description] [Solution] 每读入一个x; 把a[(x-1)/2]置为1即可; 统计1的个数 [NumberOf WA] [Reviw] [Code] /* */ ...

  8. [BestCoder Round#26] Apple 【组合数学】

    题目链接:HDOJ - 5160 题目分析 第一眼看上去,要求统计所有不同排列对答案的贡献.嗯...完全没有想法. 但是,如果我们对每个数字单独考虑,计算这个数字在总答案中的贡献,就容易多了. 对于一 ...

  9. hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏

    errors made, boundary conditions, <= vs < , decreasing vs increasing , ++, –, '0'/'1' vs 0/1 p ...

随机推荐

  1. Android5.0之CardView的使用

    CardView也是一个非常炫酷的控件,一般我们将CardView配合RecyclerView来使用,当然,CardView也可以配合ListView来使用,都是可以的.OK,我们先来看一张CardV ...

  2. Java基础知识强化之IO流笔记60:打印流 之 改进复制文本文件的案例

    1. 使用打印流改进复制文本文件的案例 2. 代码示例: package cn.itcast_03; import java.io.BufferedReader; import java.io.Buf ...

  3. Nginx高性能服务器安装、配置、运维 (2) —— Nginx安装

    三.Nginx 安装 使用SecureCRT以Root身份登录阿里云,在安装Nginx前先做好阿里云磁盘挂载 -------------- 挂载磁盘 -------------- 1.df -h #显 ...

  4. freewrap——将tcl/tk脚本转变为可执行文件

     FreeWrap可以把TCL/TK的脚本和二进制文件打包成应用程序,FreeWrap将所有的文件组合成一个单独的可执行文件.     FreeWrap的原理是把脚本和tcl/tk解释器和库文件都打包 ...

  5. java反射技术

    Class c2 = Class.forName("com.reflection.Test"); // 对类的寻找,找到一个类,注意不是对象 WifiManager mWifiMa ...

  6. js读取Excel文件数据-IE浏览器

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  7. SQL Server调优系列基础篇 - 联合运算符总

    前言 上两篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符的优化技巧,本篇我们总结联合运算符的使用方式和优化技巧. 废话少说,直接进入本篇的主题. 技术准备 基于SQL Server200 ...

  8. xcode编译运行报错纪录(持续更新)

    ---恢复内容开始--- 1. Undefined symbols for architecture i386: "_deflate", referenced from: -[NS ...

  9. iOS控件——UIView与UIImageView播放动画的实现方法

    1.UIView //初始状态 [UIView animateWithDuration:(int) animations:^{ //最终状态 }completion:^(BOOL finished){ ...

  10. 使用RequireJS优化Web应用前端

    require.js官网:http://requirejs.org/docs/download.html 一篇不错的文章:http://www.csdn.net/article/2012-09-27/ ...