跳到正文

题解

题解 UVA11401 【Triangle Counting】

tiger2005约 1 分钟
原文
文章信息与版本

文章信息

发布
2018年12月19日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 1 分钟
题目
UVA11401

版本

  1. v1当前

O(999997) O(999997)预处理,O(1)O(1)回答

分析一下

代码
ans []={ 0, 0, 0, 1, 3, 7,13,22,34,50,70,...}
dif1[]={  0, 0, 1, 2, 4, 6, 9,12,16,20,...}
dif2[]={   0, 1, 1, 2, 2, 3, 3, 4, 4,...}

好的

规律出现了

我们发现

这是一个“二阶等差数列”

在做两次差后有一定的规律

于是

我们可以用该规律构造答案数组

之后就万事大吉了

C++
#include<cstdio>
using namespace std;
typedef long long ll;
ll ans[1000001],in,now,ad;
bool aa;
int main(){
    now=1;
    aa=0;
    //预处理
    for(int i=4;i<=1000000;i++){
        ad=ad+now;
        ans[i]=ans[i-1]+ad;
        aa=!aa;
        if(!aa) ++now;
    }
    //IO
    scanf("%lld",&in);
    T:if(in<3)   return 0;//天坑(小于三就结束)
    printf("%lld\n",ans[in]);
    scanf("%lld",&in);
    goto T;
}
原文