跳到正文

题解

P1483 序列变换-题解报告

BigJoker约 3 分钟
原文
文章信息与版本

文章信息

发布
2021年12月25日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 3 分钟
获赞
3
题目
P1483

版本

  1. v1当前

1.前言

一道小水题,认真读了题后都不难。

2.思路

我会模拟!70pts70pts

第一思路是模拟,直接暴力修改,最坏的情况是当 x=1x=1 时,需要修改所有的数,明显会炸。

C++
#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;
} 

我会观察!40pts40pts

观察题面的数据范围,发现操作2的数量不超过 10410^4,想到可以用一个标记在操作1存储一下累加的值,在操作2时直接从 11 到 nn 枚举它的因数,加上标记即可,但是复杂度 101010^{10},艹,TLE了。

C++
#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;
}

我会优化!100pts100pts

直接根据因数的性质将操作2枚举的范围搞到 106\sqrt{10^6},复杂度 10710^7,AC了。

C++
#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;
}
原文
存档评论1 条抓取于