Discription

As you know, the most intelligent beings on the Earth are, of course, cows. This conclusion was reached long ago by the Martian aliens, as well as a number of other intelligent civilizations from outer space.

Sometimes cows gather into cowavans. This seems to be seasonal. But at this time the cows become passive and react poorly to external stimuli. A cowavan is a perfect target for the Martian scientific saucer, it's time for large-scale abductions, or, as the Martians say, raids. Simply put, a cowavan is a set of cows in a row.

If we number all cows in the cowavan with positive integers from 1 to n, then we can formalize the popular model of abduction, known as the (a, b)-Cowavan Raid: first they steal a cow number a, then number a + b, then — number a + 2·b, and so on, until the number of an abducted cow exceeds n. During one raid the cows are not renumbered.

The aliens would be happy to place all the cows on board of their hospitable ship, but unfortunately, the amount of cargo space is very, very limited. The researchers, knowing the mass of each cow in the cowavan, made p scenarios of the (a, b)-raid. Now they want to identify the following thing for each scenario individually: what total mass of pure beef will get on board of the ship. All the scenarios are independent, in the process of performing the calculations the cows are not being stolen.

Input

The first line contains the only positive integer n (1 ≤ n ≤ 3·105) — the number of cows in the cowavan.

The second number contains n positive integer wi, separated by spaces, where the i-th number describes the mass of the i-th cow in the cowavan (1 ≤ wi ≤ 109).

The third line contains the only positive integer p — the number of scenarios of (a, b)-raids (1 ≤ p ≤ 3·105).

Each following line contains integer parameters a and b of the corresponding scenario (1 ≤ a, b ≤ n).

Output

Print for each scenario of the (a, b)-raid the total mass of cows, that can be stolen using only this scenario.

Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams of the %I64d specificator.

Examples

Input
3
1 2 3
2
1 1
1 2
Output
6
4
Input
4
2 3 5 7
3
1 3
2 3
2 2
Output
9
3
10 比较显然的是对b 分块: b大的直接暴力算; b小的时候之前预处理一下然后直接回答询问。。。。
但是看了一下空间限制。。。。。70M。。。。这就只能开 300000 * 30 的 long long 啊。。。突然感觉分块大失败。
不过问题并不在这里,因为我们完全可以离线做, b >=sqrt(N) 的询问直接回答;b<=sqrt(N) 的询问先记下来, 然后我们再跑 sqrt(N) 次查找,每次处理出 步长为i, 从每个位置向后能够得到的牛个数。 这样一遍就是O(N) 的。。
于是总的复杂度就是 O(N * sqrt(N) + Σ [b>sqer(N)] * (N/b))
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn=300005;
const int Base=550;
struct node{ int num,B;};
vector<node> g[Base+5];
ll ans[maxn],f[maxn];
int n,a[maxn],Q; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
}
void W(ll x){ if(x>=10) W(x/10); putchar(x%10+'0');} inline void solve(){
node x;
for(int i=1;i<=Base;i++) if(g[i].size()){
memset(f,0,sizeof(f));
for(int j=n;j;j--) f[j]=a[j]+(ll)(j+i<=n?f[j+i]:0);
for(int j=g[i].size()-1;j>=0;j--){
x=g[i][j];
ans[x.num]=f[x.B];
}
}
} int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout); n=read();
for(int i=1;i<=n;i++) a[i]=read(); Q=read(); int S,T;
for(int i=1;i<=Q;i++){
S=read(),T=read();
if(T<=Base) g[T].pb((node){i,S});
else for(int j=S;j<=n;j+=T) ans[i]+=(ll)a[j];
} solve(); for(int i=1;i<=Q;i++) W(ans[i]),puts("");
return 0;
}

  

 

CodeForces - 103D Time to Raid Cowavans的更多相关文章

  1. CodeForces 103D Time to Raid Cowavans 询问分块

    Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...

  2. CodeForces 103D Time to Raid Cowavans 分块+dp

    先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下 dp[i]表示以nb为等差,i为起点的答案,可以节省nb相同的情况 #include<bits ...

  3. Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块

    题目链接: http://codeforces.com/contest/103/problem/D D. Time to Raid Cowavans time limit per test:4 sec ...

  4. CodeForces 103 D Time to Raid Cowavans

    Time to Raid Cowavans 题意:一共有n头牛, 每头牛有一个重量,m次询问, 每次询问有a,b 求出 a,a+b,a+2b的牛的重量和. 题解:对于m次询问,b>sqrt(n) ...

  5. Codeforces103D - Time to Raid Cowavans

    Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...

  6. (分块暴力)Time to Raid Cowavans CodeForces - 103D

    题意 给你一段长度为n(1 ≤ n ≤ 3·1e5)的序列,m (1 ≤ p ≤ 3·1e5)个询问,每次询问a,a+b,a+2b+...<=n的和 思路 一开始一直想也想不到怎么分,去维护哪些 ...

  7. Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块

    D. Turtles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/103/problem/D ...

  8. CodeForces 103D 分块处理

    题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...

  9. D. Time to Raid Cowavans 分块暴力,感觉关键在dp

    http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...

随机推荐

  1. 16,Python网络爬虫之Scrapy框架(CrawlSpider)

    今日概要 CrawlSpider简介 CrawlSpider使用 基于CrawlSpider爬虫文件的创建 链接提取器 规则解析器 引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话, ...

  2. leetcode 【 Merge Sorted Array 】python 实现

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  3. java中常用的几种缓存类型介绍

    在平时的开发中会经常用到缓存,比如locache.redis等,但一直没有对缓存有过比较全面的总结.下面从什么是缓存.为什么使用缓存.缓存的分类以及对每种缓存的使用分别进行分析,从而对缓存有更深入的了 ...

  4. Postgres 将查询结果同时插入数据表

    INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ... ...

  5. springmvc和struts2的区别比较

    1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...

  6. DOM的相关概念

    [前面的话]DOM全称是Document Object Model,即文档对象模型.我们常说的html文档其实就是一个DOM树,DOM操作就是在内存中找到DOM树上我们想要的DOM对象,对它的属性进行 ...

  7. NSByteCountFormatter

    可以将字节转化为我们需要的Kb或者m + (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFor ...

  8. BZOJ 2243:染色(树链剖分+区间合并线段树)

    [SDOI2011]染色Description给定一棵有n个节点的无根树和m个操作,操作有2类:1.将节点a到节点b路径上所有点都染成颜色c:2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认 ...

  9. POJ 3368:Frequent values(线段树区间合并)

    题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...

  10. [NOI2012][bzoj2879] 美食节 [费用流+动态加边]

    题面 传送门 思路 先看看这道题 修车 仔细理解一下,这两道题是不是一样的? 这道题的不同之处 但是有一个区别:本题中每一种车有多个需求,但是这个好办,连边的时候容量涨成$p\lbrack i\rbr ...