[HDOJ 5212] [BestCoder Round#39] Code 【0.0】
题目链接: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】的更多相关文章
- 暴力+降复杂度 BestCoder Round #39 1002 Mutiple
题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...
- 贪心 BestCoder Round #39 1001 Delete
题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...
- SpringBoot2整合Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the calling code 【已解决】
SpringBoot集成Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the callin ...
- 【CS Round #39 (Div. 2 only) D】Seven-segment Display
[Link]:https://csacademy.com/contest/round-39/task/seven-segment-display/ [Description] 0..9各自有一个数字, ...
- 【CS Round #39 (Div. 2 only) C】Reconstruct Sum
[Link]:https://csacademy.com/contest/round-39/task/reconstruct-sum/ [Description] 给你一个数字S; 让你找有多少对A, ...
- 【CS Round #39 (Div. 2 only) B】Circle Elimination
[Link]:https://csacademy.com/contest/round-39/task/circle-elimination/ [Description] [Solution] 把n个点 ...
- 【CS Round #39 (Div. 2 only) A】Removed Pages
[Link]: [Description] [Solution] 每读入一个x; 把a[(x-1)/2]置为1即可; 统计1的个数 [NumberOf WA] [Reviw] [Code] /* */ ...
- [BestCoder Round#26] Apple 【组合数学】
题目链接:HDOJ - 5160 题目分析 第一眼看上去,要求统计所有不同排列对答案的贡献.嗯...完全没有想法. 但是,如果我们对每个数字单独考虑,计算这个数字在总答案中的贡献,就容易多了. 对于一 ...
- 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 ...
随机推荐
- 用Java对xml文档进行遍历,更新,创建,删除
import java.io.File;import java.io.FileInputStream; import javax.imageio.stream.FileImageInputStream ...
- Asp.Net分页存储过程
SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in (select top (当前的 ...
- mysql远程连接错误提醒:2013-Lost connection to MySQL server at ‘reading initial communication packet', system error: 0
因为没有匹配/etc/hosts.allow. 解决方法: 1.在hosts.allow 文件中添加 mysqld:ALL [root@ucDB204 ~]# cat /etc/hosts.allow ...
- LaTeX 标题中使用 \bm 命令与 hyperref 的冲突
问题 当使用 hyperref 宏包时,在标题中使用 \bm 为数学符号加粗会出现错误 \documentclass{article} \usepackage{bm} \usepackage{hype ...
- 单台电脑上启动多个Modelsim图形环境窗口的简单办法(windows)
1 单台电脑上启动多个Modelsim图形环境窗口的简单办法(windows) http://blog.21ic.com/user1/3128/archives/2010/73447.html 单 ...
- I2C Verilog的实现(二)
1. 起始结束信号的判断 //--------------------------------------------- //start,stop condition judgement //---- ...
- 移动端 touch 实现 拖动元素
var homeMove = (function () { //touch自适应 var k = "ontouchend" in window ? "touchend&q ...
- 20160328 javaweb Cookie 小练习
利用cookie实现历史记录浏览: 由于是简单演示,所以直接用javabean 取代数据库了 数据存储类: package com.dzq.dao; import java.util.*; impor ...
- php中实现精确设置session过期时间的方法
http://www.jb51.net/article/52309.htm 大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设置一下sessio ...
- Ajax实现页面后台button click事件无刷新弹窗
很多人在做链接弹出新窗口的时候,都习惯用Response.Wrtite("<script>window.open('a.aspx')</script>") ...