P3987 我永远喜欢珂朵莉~ 题解

\(\Huge\text{大家好,我是 i64,我非常喜欢暴力算法,于是我用}\\\Huge\text{暴力通过了这道题。}\)
\(\Huge\text{本文并非此题正解,使用了一点点卡常技巧}\)

无优化

你可以使用幼儿园编程技巧写出这段连我小区楼底下天天翘着二郎腿抽烟的保安大爷都能写出的代码。

#include<iostream>
using namespace std;

#define int long long

const int maxn = 100010;
int a[maxn];

signed main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (m--) {
        int opt;
        cin >> opt;
        if (opt == 1) {
            int l, r, x;
            cin >> l >> r >> x;
            for (int i = l;i <= r;i++) {
                if (a[i] % x == 0) {
                    a[i] /= x;
                }
            }
        }
        if (opt == 2) {
            int l, r;
            cin >> l >> r;
            int ans = 0;
            for (int i = l;i <= r;i++) {
                ans += a[i];
            }
            cout << ans << endl;
        }
    }
    return 0;
}

不开 O2,喜提 15pts 还有这时间就离谱,头回看到上 1min 的(

开了 O2,喜提 36pts

尝试用一下快读快写

众所周知,cin&cout 的速度比我家门前公园里散步的老太太跑步都慢,所以试一下快读快写。


#include<iostream>
using namespace std;

inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void write(long long x) {
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}

const int maxn = 100010;
int a[maxn];
int opt;
int n, m;
int l, r, x;
long long ans = 0;
int i;

signed main() {
    n = read(), m = read();
    for (i = 1; i <= n; i++) {
        a[i] = read();
    }
    while (m--) {
        opt = read();
        if (opt == 1) {
            l = read(), r = read(), x = read();
            for (int i = l;i <= r;i++) {
                if (a[i] % x == 0) {
                    a[i] /= x;
                }
            }
        }
        if (opt == 2) {
            l = read(), r = read();
            ans = 0;
            for (int i = l;i <= r;i++) {
                ans += a[i];
            }
            write(ans);
            puts("");
        }
    }
    return 0;
}

喜提 88pts,我们离 AC 更近一步了!

尝试一下循环展开

众所周知,纯 for 循环慢的一批,所以我们尝试展开一下。

#include <iostream>
using namespace std;

inline int read() {
	register int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}
void write(register long long x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
	return;
}

const int maxn = 100010;
int a[maxn];

signed main() {
	register int opt;
	register int n, m;
	register int l, r, x;
	register long long sum = 0;
	register int i;
	n = read(), m = read();
	for (i = 1; i <= n; i++) {
		a[i] = read();
	}
	while (m--) {
		opt = read();
		if (opt == 1) {
			l = read(), r = read(), x = read();
			for (i = l; i <= r - 5; i += 6) {
				!(a[i] % x) ? a[i] /= x : 1;
				!(a[i + 1] % x) ? a[i + 1] /= x : 1;
				!(a[i + 2] % x) ? a[i + 2] /= x : 1;
				!(a[i + 3] % x) ? a[i + 3] /= x : 1;
				!(a[i + 4] % x) ? a[i + 4] /= x : 1;
				!(a[i + 5] % x) ? a[i + 5] /= x : 1;
			}
			while (r - i + 1) {
				!(a[i] % x) ? a[i] /= x : 1;
				i++;
			}
		}
		if (opt == 2) {
			sum = 0;
			l = read(), r = read();
			for (i = l; i <= r - 5; i += 6) {
				sum +=
					a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5];
			}
			while (r - i + 1) {
				sum += a[i];
				i++;
			}
			write(sum);
			puts("");
		}
	}
	return 0;
}

耶,97 pts 了,就差一个点就 AC 了!!!

在现在这个时候,我们就会对这道题感到困惑,你很想找到背后那个美妙的解法,你知道那是不是正解,但你仍然想掌握它。否则你会不安,你会失眠,你会深夜不睡坐在电脑前对着洛谷和 IDE 不停的码代码,你休息的时候会长年累月泡机房。你不知道怎么入手,你觉得总是差那么一点儿就抓住了,但是每次抓到的都是空气……

\(\Huge\text{这时,我们就需要用到一个名为“题解”的东西了!!!}\)

点进去,你看见了一篇 Blog,并打开它。发现这个 Bloger 是王熙文大佬,而且跟你的思路很像!

他想到了要特判 \(x\)\(1\)\(2\) 的情况!

如果 \(x=1\),则直接退出!如果 \(x=2\),你就可以使用位运算让你的这份代码变得更快!!!

于是你……

最后一击!

#include <iostream>
using namespace std;

inline int read() {
	register int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}
void write(register long long x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
	return;
}

const int maxn = 100010;
int a[maxn];

signed main() {
	register int opt;
	register int n, m;
	register int l, r, x;
	register long long sum = 0;
	register int i;
	n = read(), m = read();
	for (i = 1; i <= n; i++) {
		a[i] = read();
	}
	while (m--) {
		opt = read();
		if (opt == 1) {
			l = read(), r = read(), x = read();
			if (x == 1) continue;
			if (x == 2) {
				for (i = l; i <= r - 5; i += 6) {
					!(a[i] & 1) ? a[i] >>= 1 : 1;
					!(a[i + 1] & 1) ? a[i + 1] >>= 1 : 1;
					!(a[i + 2] & 1) ? a[i + 2] >>= 1 : 1;
					!(a[i + 3] & 1) ? a[i + 3] >>= 1 : 1;
					!(a[i + 4] & 1) ? a[i + 4] >>= 1 : 1;
					!(a[i + 5] & 1) ? a[i + 5] >>= 1 : 1;
				}
				while (r - i + 1) {
					!(a[i] & 1) ? a[i] >>= 1 : 1;
					i++;
				}
				continue;
			}
			for (i = l; i <= r - 5; i += 6) {
				!(a[i] % x) ? a[i] /= x : 1;
				!(a[i + 1] % x) ? a[i + 1] /= x : 1;
				!(a[i + 2] % x) ? a[i + 2] /= x : 1;
				!(a[i + 3] % x) ? a[i + 3] /= x : 1;
				!(a[i + 4] % x) ? a[i + 4] /= x : 1;
				!(a[i + 5] % x) ? a[i + 5] /= x : 1;
			}
			while (r - i + 1) {
				!(a[i] % x) ? a[i] /= x : 1;
				i++;
			}
		}
		if (opt == 2) {
			sum = 0;
			l = read(), r = read();
			for (i = l; i <= r - 5; i += 6) {
				sum +=
					a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5];
			}
			while (r - i + 1) {
				sum += a[i];
				i++;
			}
			write(sum);
			puts("");
		}
	}
	return 0;
}

恭喜,你 AC 了!!!

本篇文章较为魔怔,纯属考完期末考之后的发泄(
posted @   MituFun  阅读(83)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示