跳到正文

题解

题解:P12600 连号子序列数

jiayunli约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月25日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
题目
P12600

版本

  1. v1当前

五年级蒟蒻题解,大佬见谅。

第一句:本题是P8600 [蓝桥杯 2013 省 B] 连号区间数的加强版一看就是唬人的,直接忽略。

思路

这个算一下就可以推出来:不同连号子序列的数目为 1+2+3+...+n=n×(n+1)2 1 + 2 + 3 + ... + n = \frac{n\times(n+1)}{2}。

代码

三种代码都能过:

C++
#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n;
int main(){
	ios::sync_with_stdio(false);//速读
	cin.tie(0),cout.tie(0);
	cin>>n;
	cout<<n*(n+1)/2<<endl;
	return 0;//完美收尾
}
C++
#include<bits/stdc++.h>
#define endl '\n'
#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n,cnt;
int main(){
	ios::sync_with_stdio(false);//速读
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(ll i=1,x;i<=n;i++){
		cin>>x;
		cnt+=x;
	}
	cout<<cnt<<endl;
	return 0;//完美收尾
}
C++
#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n,cnt;
int main(){
	ios::sync_with_stdio(false);//速读
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(ll i=1;i<=n;i++){
		cnt+=i;
	}
	cout<<cnt<<endl;
	return 0;//完美收尾
}
原文