hust1601 校赛

Shepherd

Time Limit: 2 Sec   Memory Limit: 64 MB
Submissions: 151   Solved: 6

Description

Hehe keeps a flock of sheep, numbered from 1 to n and each with a weightwi. To keep the sheep healthy, he prepared some training for his sheep.Everytime he selects a pair of numbers (a,b), and chooses the sheep with number a, a+b, a+2b, … to get trained. For the distance between the sheepfold and the training site is too far, he needs to arrange a truck with appropriate loading capability to transport those sheep. So he wants to know the total weight of the sheep he selected each time, and he finds you to help him.

Input

There’re several test cases. For each case:
The first line contains a positive integer n (1≤n≤10^5)---the number of sheepHehe keeps.
The second line contains n positive integer wi(1≤n≤10^9), separated by spaces, where thei-th number describes the weight of the i-th sheep.
The third line contains a positive integer q (1≤q≤10^5)---the number of training plansHehe prepared.
Each following line contains integer parameters a and b (1≤a,b≤n)of the corresponding plan.

Output

For each plan (the same order in the input), print the total weight of sheep selected.

Sample Input

5
1 2 3 4 5
3
1 1
2 2
3 3

Sample Output

15
6
3


思路 :如果b很大就暴力  b很小就打表    用根号下10^5做b的大小界限


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long
const int MAX=100000+100;
/*struct haha
{
	int s;
	int b;
	int id;
}map[MAX];*/
struct haha
{
	int s;
	int b;
	int id;
	bool operator<(const haha &x)const {
		return b<x.b;
	}
}map[MAX];
ll a[MAX];
ll ans[MAX],mid[MAX];
/*int cmp(const void *a,const void *b)
{
	return (*(struct haha *)b).b-(*(struct haha *)a).b;
}*/
int main()
{
	int n,i,j,k,m,l,t;
	while(scanf("%d",&n)!=EOF)
	{
          for(i=0;i<n;i++)
			  scanf("%lld",&a[i]);
		  scanf("%d",&m);
		  for(i=0;i<m;i++)
		  {
			   scanf("%d %d",&map[i].s,&map[i].b);
			   map[i].id=i;
		  }
		 // qsort(map,n,sizeof(map[0]),cmp);
		   sort(map,map+m);//用qsort就超时 用sort就过了  坑爹啊  并且只能从大到小排列不知道为什么
		  for(i=j=0;i<m&&map[i].b<400;i=j)//400是对10的5次方取根号大约得到的数  别用根号下m哦 b是和最大上限
		  {
			   while(j<m&&map[i].b==map[j].b) j++;
			   for(l=n-1;l>=0;l--)
			   {
				   if(l+map[i].b<n)  mid[l]=mid[l+map[i].b]+a[l];
				   else mid[l]=a[l];
			   }
			   for(t=i;t<j;t++)
				   ans[map[t].id]=mid[map[t].s-1];

		  }
		  while(i<m)
		  {
			  ll tmp=0;
			  for(k=map[i].s-1;k<n;k+=map[i].b) tmp+=a[k];
			  ans[map[i++].id]=tmp;
		  }
		  for(i=0;i<m;i++) printf("%lld\n",ans[i]);
	}
	return 0;
}



你可能感兴趣的:(hust1601 校赛)