【CodeForces - 1207F --- Remainder Problem 】

【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) iR(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序列,有两种操作:

  1. a[x]+=y;
  2. 输出满足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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值