首页 技术 正文
技术 2022年11月18日
0 收藏 387 点赞 3,557 浏览 2982 个字

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.

CodeForces – 103D  Time to Raid Cowavans

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;
}

  

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900