P1483 序列变换-题解报告

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值