Codeforces Round #80 Div.1 D
思路:考虑离线操作,以y为关键字排序,对于y相同的一起操作,然后考虑y的范围,当y<=sqrt(n)时,直接O(n)预处理出f[x]表示f[x]+f[x+y]+f[x+2*y]+..+f[x+k*y]的答案,然后这样的y显然不超过sqrt(n)个,复杂度也就是O(n*sqrt(n))的;如果y>sqrt(n),那么这样直接暴力统计答案,因为答案的项数也显然不会超过sqrt(n),这样整个算法的时间复杂度就是O(n*sqrt(n))。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 300005 int n,Q;
int a[maxn];
long long f[maxn],ans[maxn]; struct query{
int x,y,id;
bool operator <(const query &a)const{return y<a.y;}
}q[maxn]; int read(){
int x=;char ch=getchar();
for (;ch<''||ch>'';ch=getchar());
for (;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
return x;
} int main(){
n=read();int size=sqrt(n);
for (int i=;i<=n;i++) a[i]=read();
Q=read();
for (int i=;i<=Q;i++) q[i].x=read(),q[i].y=read(),q[i].id=i;
sort(q+,q+Q+);
for (int i=;i<=Q;i++){
if (q[i].y<=size){
int y=q[i].y;
if (y!=q[i-].y) for (int j=n;j;j--) f[j]=(j+y>n?a[j]:f[j+y]+a[j]);
ans[q[i].id]=f[q[i].x];
}
else{
int x=q[i].x,y=q[i].y,id=q[i].id;
while (x<=n) ans[id]+=a[x],x+=y;
}
}
for (int i=;i<=Q;i++) printf("%I64d\n",ans[i]);
return ;
}
Codeforces Round #80 Div.1 D的更多相关文章
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Round #491 (Div. 2)
Codeforces Round #491 (Div. 2) https://codeforces.com/contest/991 A #include<bits/stdc++.h> us ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
随机推荐
- HDU-4419 Colourful Rectangle 矩形多面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 利用二进制,R为1.G为2.B为4,然后通过异或运算可以得到其它组合颜色.建立7颗线段树,每颗线 ...
- 潜语义分析(Latent Semantic Analysis)
LSI(Latent semantic indexing, 潜语义索引)和LSA(Latent semantic analysis,潜语义分析)这两个名字其实是一回事.我们这里称为LSA. LSA源自 ...
- iOS开发中常见的语句@synthesize obj = _obj 的意义详解
我们在进行iOS开发时,经常会在类的声明部分看见类似于@synthesize window=_window; 的语句,那么,这个window是什么,_ window又是什么,两个东西分别怎么用,这是一 ...
- linux下编译安卓ffmpeg
本次编译属于2013年6月项目的一部分,重新修改使用. 为统一工程版本 ffmpeg版本为1.2.1 本次的目录结构为 工程目录/jni/Android.mk 工程目录/jni/Application ...
- sql server 分布式事务
使用分布式事务刚好可以解决集群同时更新多台SQL SERVER数据库,要么全部成功,要么全部回滚的需要. 原来微软早考虑到此方面的问题了. 下面背书,贴出微软官网上面的帮助文档: 分布式事务跨越两个或 ...
- iframe与include的区别
iframe与include区别和使用问题 1.iframe可以用在静态和动态页面,include只能用在动态页面. 2.iframe是视图级组合,include是代码级组合. 3.iframe独立成 ...
- Java用OpenOffice将word转换为PDF
一. 软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...
- maven 通过nexus创建工厂的配置
访问中央工厂的地址:http://localhost:8081/nexus/index.html#welcome admin.admin123 查找到自己配置的maven仓库的位置,编辑setti ...
- JavaWeb国际化
软件的国际化: 软件在不同的地方,适应不同的风格: 中国: 显示中文,以及服务符合中国习惯的文本字符串! 美国: 显示英文,以及服务符合他国习惯的文本字符串! 这种软件,就叫国际化的软件! 如何做到国 ...
- 递归小demo(1-100的和)
public class demo1 { public static void main(String[] args) { //初始值为100 int n = 100; //调用number方法,返回 ...