https://codeforces.com/contest/1207/problem/F
F. Remainder Problem
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are given an array aa consisting of 500000500000 integers (numbered from 11 to 500000500000). Initially all elements of aa are zero.
You have to process two types of queries to this array:
- 11 xx yy — increase axax by yy;
- 22 xx yy — compute ∑i∈R(x,y)ai∑i∈R(x,y)ai, where R(x,y)R(x,y) is the set of all integers from 11 to 500000500000 which have remainder yy modulo xx.
Can you process all the queries?
Input
The first line contains one integer qq (1≤q≤5000001≤q≤500000) — the number of queries.
Then qq lines follow, each describing a query. The ii-th line contains three integers titi, xixi and yiyi (1≤ti≤21≤ti≤2). If ti=1ti=1, then it is a query of the first type, 1≤xi≤5000001≤xi≤500000, and −1000≤yi≤1000−1000≤yi≤1000. If ti=2ti=2, then it it a query of the second type, 1≤xi≤5000001≤xi≤500000, and 0≤yi<xi0≤yi<xi.
It is guaranteed that there will be at least one query of type 22.
Output
For each query of type 22 print one integer — the answer to it.
Example
input
Copy
5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0
output
Copy
4 4 0
题意:
一个长度为50000的数组a[],初始为0,有两个操作:
- 1 x y:a[x]+=y
- 2 x y: if(i%x==y) sum+=a[i],输出sum
分析:
因为i%x=y,正好满足分块的性质,所以用分块去做
sqrt(500000)<=710,
所以对于模数x<=710的直接用b[i][y]保存答案,表示模i为y的答案,对于y大于710,暴力跑一遍即可(sqrt(n))。
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
const int K = 710;
#define LL long long
int a[N];
int b[K][K];
int main()
{
int q;
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
int t, x, y;
scanf("%d %d %d", &t, &x, &y);
if(t == 1)
{
a[x]+=y;
for(int i=1; i<K; i++)
b[i][x%i]+=y;
}
else
{
LL res=0;
if(x>=K)
{
for(int i=y; i<=N; i+=x)
{
res+=a[i];
}
cout<<res<<endl;
}
else
{
cout<<b[x][y]<<endl;
}
}
}
return 0;
}