题解 CF103A 【Testing Pants for Sadness】

解题思路

首先要知道每次尝试需要重新再做一遍(要是我就没有这个耐力),重新做就是把已经做过的题数+1重复选项数-1遍,加上最后的选项数(不理解可以手动模拟一下,还是用实打实的手写吧,我拿电脑不便于记录每步的状态)。于是可得此递推式:

   		 n
	ans=Σ(a[i]-1)*(i-1)+a[i]
  
  	 	i=1

代码实现

时间复杂度O(n),空间复杂度O(1)

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
//上面下面的是为了偷懒
#define go(i,j,n,k) for(register ll i=j;i<=n;i+=k)
inline ll read(){//读入优化,有没有无所谓,用cin就可以
	ll x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll n,a,ans=0,num;
int main(){
	n=read();
	go(i,1,n,1){
		a=read();//输入,顺便直接求和,减少数组与另一个循环
		ans+=(a-1)*(i-1)+a;//递推式
	}
	cout<<ans;
	return 0;
}
NOIP2018并不是结束,而是开始
原文地址:https://www.cnblogs.com/yizimi/p/10056211.html