【CodeForces - 1207F --- Remainder Problem 】
Description
You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero.
You have to process two types of queries to this array:
1 x y — increase ax by y;
2 x y — compute
∑
i
∈
R
(
x
,
y
)
∑i∈R(x,y)
∑i∈R(x,y)ai, where R(x,y) is the set of all integers from 1 to 500000 which have remainder y modulo x.
Can you process all the queries?
Input
The first line contains one integer q (1≤q≤500000) — the number of queries.
Then q lines follow, each describing a query. The i-th line contains three integers ti, xi and yi (1≤ti≤2). If ti=1, then it is a query of the first type, 1≤xi≤500000, and −1000≤yi≤1000. If ti=2, then it it a query of the second type, 1≤xi≤500000, and 0≤yi<xi.
It is guaranteed that there will be at least one query of type 2.
Output
For each query of type 2 print one integer — the answer to it.
Sample Input
5
1 3 4
2 3 0
2 4 3
1 4 -4
2 1 0
Sample Output
4
4
0
题意
刚开始有一个长度为500000的全0序列,有两种操作:
- a[x]+=y;
- 输出满足i%x==y条件,所有a[i]的和。
解题思路
对于第一种操作a[x]+=y直接写就行了,但是为了能够减少程序运行时间,只需花一个n=700的循环更新一下ans[][]数组,ans[x][y]是指满足i%x==y的所有a[i]之和。之所以用700是因为接近于根号500000.如果查询的x大于700那就用循环一步一步的加起来就行了。注意用long long.
AC代码:
#include <stdio.h>
typedef long long ll;
ll a[500005];
ll ans[710][710];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int flag,x,y;
scanf("%d%d%d",&flag,&x,&y);
if(flag==1)
{
a[x]+=y;
for(int i=1;i<=700;i++) ans[i][x%i]+=y;
}
else if(flag==2)
{
if(x<=700)
printf("%I64d\n",ans[x][y]);
else
{
ll res = 0;
for(ll i=y;i<=500000;i+=x) res+=a[i];
printf("%I64d\n",res);
}
}
}
return 0;
}