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

  1. 3
    1 2 3
    2
    1 1
    1 2

Sample Output

  1. 6
    4

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年国家集训队论文《根号算法,不只是分块》

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define test freopen("test.txt","r",stdin)
  22. #define maxn 400001
  23. #define mod 10007
  24. #define eps 1e-9
  25. int Num;
  26. char CH[];
  27. //const int inf=0x7fffffff; //нчоч╢С
  28. const int inf=0x3f3f3f3f;
  29. inline ll read()
  30. {
  31. ll x=,f=;char ch=getchar();
  32. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  33. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  34. return x*f;
  35. }
  36. inline void P(int x)
  37. {
  38. Num=;if(!x){putchar('');puts("");return;}
  39. while(x>)CH[++Num]=x%,x/=;
  40. while(Num)putchar(CH[Num--]+);
  41. puts("");
  42. }
  43. //**************************************************************************************
  44.  
  45. ll a[maxn],n,m;
  46. int cx[maxn],cy[maxn];
  47. ll ans[maxn];
  48. ll dp[maxn];
  49. int kiss=;
  50. vector<int> d[];
  51. int main()
  52. {
  53. //test;
  54. n=read();
  55. for(int i=;i<=n;i++)
  56. a[i]=read();
  57. int m=read();
  58. for(int i=;i<=m;i++)
  59. {
  60. cx[i]=read(),cy[i]=read();
  61. if(cy[i]<kiss)
  62. d[cy[i]].push_back(i);
  63. else
  64. {
  65. ll ans1=;
  66. for(int j=cx[i];j<=n;j+=cy[i])
  67. {
  68. ans1+=a[j];
  69. }
  70. ans[i]=ans1;
  71. }
  72. }
  73. for(int i=;i<kiss;i++)
  74. {
  75. if(d[i].size())
  76. {
  77. for(int j=n;j;j--)
  78. {
  79. if(j+i>n)
  80. dp[j]=a[j];
  81. else
  82. dp[j]=dp[j+i]+a[j];
  83. }
  84. for(int j=;j<d[i].size();j++)
  85. ans[d[i][j]]=dp[cx[d[i][j]]];
  86. }
  87. }
  88. for(int i=;i<=m;i++)
  89. cout<<ans[i]<<endl;
  90. }

Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块的更多相关文章

  1. 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 ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. 分治思想 特别常用 Codeforces Beta Round #80 (Div. 1 Only) D

    D. Time to Raid Cowavans time limit per test 4 seconds memory limit per test 70 megabytes input stan ...

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

    Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...

  5. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. PXC加入新节点避免SST时grastate.dat文件内容的修改问题

    PXC加入新节点避免SST时grastate.dat文件内容的修改问题 在主从同步完成并关闭实例后,需要修改grastate.dat中的seqno:到底应该填已经执行过最后的XID号(Executed ...

  2. webstrom 里面使用github

    1.输入github的账号和密码,点击登录 2.复制github的项目地址,现在clone就行了

  3. Python中的raw_input()和input()

    raw_input()和input()都是python中的内建函数,用于读取控制台用户的输入,但有所区别: [nr@localhost conf]$ python Python 2.7.5 (defa ...

  4. .htaccess教程:简介、访问控制、验证、目录浏览控制

    一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...

  5. linux -j 4

    把源码编译成可执行的二进制文件, 4为服务器内核数

  6. 【转】doxygen+graphviz生成工程中的类继承树及函数调用图

    转自----hequn8128 在阅读代码量比较多的项目时,类的继承树和函数调用图能够直观地向我们显示类之间或者函数之间的各种关系,方便我们了解程序的整体框架,很多时候可以起到事半功倍的作用.这里尝试 ...

  7. HTML常用标签及其属性

    基本 <html>…</html> 定义 HTML 文档 <head>…</head> 文档的信息 <meta> HTML 文档的元信息 & ...

  8. CSU 1240 低调,低调。

    原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1240 这道题已经做了很久了,加入给足够大的内存,谁都会做. 在一个数列中找一个只出现一次 ...

  9. 【PAT】1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  10. js监测设备类型【安卓,ios,苹果微信,电脑pc】

    话不多说上代码: 1.判断是不是微信 function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/Mic ...