1.前言
一道小水题,认真读了题后都不难。
2.思路
我会模拟! 70 p t s 70pts 70pts
第一思路是模拟,直接暴力修改,最坏的情况是当 x = 1 x=1 x=1 时,需要修改所有的数,明显会炸。
#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000005];
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
while(m--){
int pd;
scanf("%d",&pd);
if(pd==1){
int x,y;
scanf("%d %d",&x,&y);
for(int i=1;i<=n/x;i++)
a[i*x]+=y;
}
else{
int ans;
scanf("%d",&ans);
printf("%d\n",a[ans]);
}
}
return 0;
}
我会观察! 40 p t s 40pts 40pts
观察题面的数据范围,发现操作2的数量不超过 1 0 4 10^4 104,想到可以用一个标记在操作1存储一下累加的值,在操作2时直接从 1 1 1 到 n n n 枚举它的因数,加上标记即可,但是复杂度 1 0 10 10^{10} 1010,艹,TLE了。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,tag[N],a[N];
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
while(m--){
int opt,x,y;
scanf("%d %d",&opt,&x);
if(opt==1){
scanf("%d",&y);
tag[x]+=y;
}
else{
int res=a[x];
for(int i=1;i<=x;i++)
if(x%i==0) res+=tag[i];
printf("%d\n",res);
}
}
return 0;
}
我会优化! 100 p t s 100pts 100pts
直接根据因数的性质将操作2枚举的范围搞到 1 0 6 \sqrt{10^6} 106,复杂度 1 0 7 10^7 107,AC了。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,tag[N],a[N];
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
while(m--){
int opt,x,y;
scanf("%d %d",&opt,&x);
if(opt==1){
scanf("%d",&y);
tag[x]+=y;
}
else{
int res=a[x];
for(int i=1;i<=sqrt(x);i++)
if(x%i==0){
res+=tag[i];
if(x/i!=i) res+=tag[x/i];
}
printf("%d\n",res);
}
}
return 0;
}