D. Turtles

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/103/problem/D

Description

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.

Sample Input

31 2 321 11 2

Sample Output

64

HINT

题意

给你N个数,然后每次询问给你(x,y),求a[x]+a[x+y]+a[x+2*y]+a[x+3*y]的和是多少

题解:

分块做,把cy[i]分成sqrt(n),把小于的都预存起来,大于的就直接暴力就好

小的,我们就可以利用 dp的思想搞定

然后复杂度O(sqrt(n))

详情见:2014年国家集训队论文《根号算法,不只是分块》

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 400001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline void P(int x)
{Num=0;if(!x){putchar('0');puts("");return;}while(x>0)CH[++Num]=x%10,x/=10;while(Num)putchar(CH[Num--]+48);puts("");
}
//**************************************************************************************

ll a[maxn],n,m;
int cx[maxn],cy[maxn];
ll ans[maxn];
ll dp[maxn];
int kiss=600;
vector<int> d[600];
int main()
{//test;n=read();for(int i=1;i<=n;i++)a[i]=read();int m=read();for(int i=1;i<=m;i++){cx[i]=read(),cy[i]=read();if(cy[i]<kiss)d[cy[i]].push_back(i);else{ll ans1=0;for(int j=cx[i];j<=n;j+=cy[i]){ans1+=a[j];}ans[i]=ans1;}}for(int i=1;i<kiss;i++){if(d[i].size()){for(int j=n;j;j--){if(j+i>n)dp[j]=a[j];elsedp[j]=dp[j+i]+a[j];}for(int j=0;j<d[i].size();j++)ans[d[i][j]]=dp[cx[d[i][j]]];}}for(int i=1;i<=m;i++)cout<<ans[i]<<endl;
}

转载于:https://www.cnblogs.com/qscqesze/p/4535642.html

Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块相关推荐

  1. Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新

    http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...

  2. Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)

    题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...

  3. Codeforces Beta Round #4 (Div. 2 Only)

    Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  4. Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力

    A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...

  5. Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

    D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...

  6. Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

    B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...

  7. Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心

    D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...

  8. Codeforces Beta Round #9 (Div. 2 Only) C. Hexadecimal's Numbers dfs

    C. Hexadecimal's Numbers 题目连接: http://www.codeforces.com/contest/9/problem/C Description One beautif ...

  9. Codeforces Beta Round #16 (Div. 2 Only)【未完结】

    2022.3.9 题目地址:https://codeforces.com/contest/16 目录 A. Flag[模拟] B. Burglar and Matches[贪心] C. Monitor ...

  10. Codeforces Beta Round #14 (Div. 2)【未完结】

    2022.3.8 题单地址:https://codeforces.com/contest/14 目录 A. Letter B. Young Photographer[差分] C. Four Segme ...

最新文章

  1. open wrt 跟换主题_Openwrt编译进阶-修改密码、路由连接数、时区及主题
  2. VC++ VS2010 error LNK1123 转换到 COFF 期间失败 怎么办
  3. ORACLE数据库基本操作命令
  4. leader选举的源码分析-startLeaderElection
  5. 无监督学习和监督学习的区别
  6. Restful HMAC认证
  7. Roslyn 使用 Directory.Build.props 管理多个项目配置
  8. 【树链剖分】染色(luogu 2486/金牌导航 树链剖分-3)
  9. python traceback_深入学习Python列表(第一部分)
  10. 苹果手机连接电脑一直噔噔蹬的响,而且没有反应
  11. Hibernate----面试题
  12. 终于知道什么叫BSS段
  13. google手机连接wifi后提示“无法连接互联网“的原因和解决方法
  14. C程序设计——图片文件合成器
  15. Authorization Basic认证 笔记
  16. 毕业后,他年薪百万,我年薪刚破十万,人和人之间的差距怎么这么大?
  17. Excel 文本转数值的方法
  18. NCE4 L5 Youth
  19. ## Python笔记
  20. 小龟视频APP-插件打包-v1.6.x反编译教程及未加固apk包ios最新版文件分享

热门文章

  1. 力扣题目系列:290. 单词规律
  2. mysql中的转换类型数据类型_mysql数据类型转换
  3. nagios 监控slave(check_mysql_health插件)
  4. Java 经典习题-初学
  5. Java必知必会之socket
  6. 树莓派学习路程No.1 树莓派系统安装与登录 更换软件源 配置wifi
  7. 你知道url中的特殊符号含义么
  8. centos创建本地yum仓库
  9. 将Windows网络适配器共享网络的ip:192.168.137.1 改为其他IP
  10. 出招挽留欧盟公民 英国政府推出“身份申请App”