Educational Codeforces Round 14 - F (codeforces 691F)
题目链接:http://codeforces.com/problemset/problem/691/F
题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p
数据范围:2≤n≤10^6, 1≤ai≤3*10^6,1≤m≤10^6, 1≤p≤3*10^6
解题思路:比赛的时候比较naive的思路是把n中的数字排序去了重之后,对于每个p,最多枚举√p步,就能得到答案。而这个naive的思路是O(p√p)的,结果T了。
后来百思不得其解,去看了官方的解答。感觉是一种很有必要总结的思路。思路的模型是埃氏筛素数。
筛素数中枚举到一个素数pr,我们就把MAX范围内的pr的倍数依次搭上标记。这样做看似暴力,实际上复杂度近乎O(n)(其实是O(MAX/p1+MAX/p2..MAX/pk))
回到这道题目,完全可以借鉴上述思路,从1-MAX枚举a,再从1-MAX/a枚举另一半b,记n个数中乘积等于i的对数ans[i],那么就有ans[a*b] += cnt[a]*cnt[b];其中cnt[i]表示n个数中i这个数出现了多少次。仔细分析复杂度的话是O(MAX/1+MAX/2+MAX/3+...+MAX/MAX),事实上,这个东西是接近O(MAXlogMAX)的,这种思想在处理一些数论问题中同样很有借鉴意义。我认为这种思路复杂度的支撑点在于他有一个上限MAX,并且内部的操作是相乘。
最后得到了ans[i]之后取个前缀和就得到了n个数中乘积<p的对数,用总对数一减就得到了答案。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL; const int MaxN = 3e6;
int n, m, MaX;
int a[MaxN + ], p[MaxN + ];
LL cnt[MaxN + ], sum[MaxN + ]; int main()
{
while (~scanf("%d", &n)) {
memset(cnt, , sizeof(cnt));
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++)
scanf("%d", &a[i]), cnt[a[i]]++;
scanf("%d", &m); MaX = -( << );
for (int i = ; i <= m; i++)
scanf("%d", &p[i]), MaX = max(MaX, p[i]);
for (int i = ; i <= MaX; i++)
for (int j = ; i * j <= MaX; j++)
if (i != j) sum[i * j] += cnt[i] * cnt[j];
else sum[i * j] += cnt[i] * cnt[i] - cnt[i];
for (int i = ; i <= MaX; i++) sum[i] += sum[i - ];
for (int i = ; i <= m; i++)
printf("%I64d\n", (LL)n * (n - ) - sum[p[i] - ]);
}
}
Educational Codeforces Round 14 - F (codeforces 691F)的更多相关文章
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- 【Codeforces Round 1137】Codeforces #545 (Div. 1)
Codeforces Round 1137 这场比赛做了\(A\).\(B\),排名\(376\). 主要是\(A\)题做的时间又长又交了两次\(wa4\)的. 这两次错误的提交是因为我第一开始想的求 ...
- 【Codeforces Round 1114】Codeforces #538 (Div. 2)
Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...
- 【Codeforces Round 1110】Codeforces Global Round 1
Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
随机推荐
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- css雪碧图制作
使用css背景合并工具cssSprite 工具下载链接: http://download.csdn.net/download/wx247919365/8741243 1.选择文件 2.生成雪碧图 3. ...
- MATLAB教程
基本操作:https://www.w3cschool.cn/matlab/ MATLAB2018a下载安装教程http://www.zhanshaoyi.com/6938.html
- xshell连接不上
1.排查道路通不通 ping baidu.com 是否通畅,如果不通 2.排查DNS ping 223.5.5.5 地址解释是否通畅 3.是否有劫财劫色的 检查防火墙 selinux是否 关 ...
- 分布式事务框架-fescar
https://github.com/alibaba/fescar/wiki/%E6%A6%82%E8%A7%88?spm=5176.11156381.0.0.b9f85ceegUXvCC
- hxq的库
在页面中使用 可以调取html模板 /** * Created by DY040 on 2017/10/31. */ var hxq = { init: function () { var self ...
- java——线程的wait()和notify()
这是一个关于生产者和消费者的线程通信的例子: package thread_test; public class PCThread { public static void main(String[] ...
- 如何在eclipse使用git插件
how to commit file to remote? --> Team --> Synchronize Workspace --> add to index --> co ...
- input元素的blur事件与定位在其上面的元素的点击(click)事件冲突的解决方法
在登录和注册框中,在input上定位一个清空内容的按钮. 但是给按钮的单击事件不生效. 解决的办法: 在blur的回调函数中加一个定时器,延迟blur回调函数的执行时间,这样的话虽然在点击div的时候 ...
- Unity GetComponents获取组件
Component[] componments2 = gameObject.GetComponents<Component>(); Debug.Log("componments2 ...