Remainder Problem

F. Remainder Problem

This is not difficult, just look at the study have no idea chunked

Ideas: with a ans[i][j]to record all k=(1~5e5)in all a[k]%i==jand, at the time of the query can achieve complexity bit O (1)

Of course, because a lot of data, can not be divided up into many pieces, but also is not necessary to be divided into many blocks, because if during the 2time of the operation, if x=1e5,y=1e5-1, in the data range then only the number of five to meet the conditions, can each skipped xcounting the number of value and the operation:

ll Ans=0;
for(int i=y;i<=int(5e5);i+=x) Ans+=a[i];
cout<<Ans<<endl;

Also: also found, in line when the output transducer cout<<'\n'than cout<<endlmany pieces, even double the difference, because cout<<endleven empty the cache, it will be a little slow

Code:

// Created by CAD on 2019/8/24.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int blo=280;
const int maxn=5e5+5;
ll ans[blo+5][blo+5];
int a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t; cin>>t;
    while(t--)
    {
        int op,x,y; cin>>op>>x>>y;
        if(op==1)
        {
            a[x]+=y;
            for(int i=1;i<=blo;++i) ans[i][x%i]+=y;
        }
        else
        {
            if(x<=blo) cout<<ans[x][y]<<'\n';
            else
            {
                ll Ans=0;
                for(int i=y;i<=int(5e5);i+=x) Ans+=a[i];
                cout<<Ans<<'\n';
            }
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/CADCADCAD/p/11404517.html