题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5459
题目来源:2015沈阳网络赛第三题
简要题意: s1=c,s2=ff,si=si−2+si−1 ,求 sn 中 c 之间的距离之和。
数据范围:
1⩽T⩽100;3⩽n⩽201314
题解
模数 530600414 是一个妹纸的QQ,看大神花式出题秀恩爱。
首先考虑距离之间肯定是有递推关系。
disi−1+disi−2 有了之后就差两段之间的c的距离之和。
而我们很好统计出长度和c的个数。
再进一步考虑,我们若是知道一段中c到左端的距离之和和到右端的距离之后就可以得到两段之间的c的距离之和了。
实现
维护五个数组
- l 表示到左边距离之和
r 表示到右边距离+1之和(方便计算)- len 表示长度
- cnt 表示c的个数
- res 表示结果,即距离之和
其中的递推关系画个图基本就能想明白了,代码中有就不赘述了。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 201444;
const LL MOD = 530600414;
LL len[N];
LL cnt[N];
LL l[N];
LL r[N];
LL res[N];
void init(int n) {
len[3] = 3, len[4] = 5;
l[4] = 2;
r[3] = r[4] = 3;
cnt[3] = cnt[4] = 1;
for (int i = 5; i <= n; i++) {
cnt[i] = (cnt[i-1] + cnt[i-2]) % MOD;
len[i] = (len[i-1] + len[i-2]) % MOD;
l[i] = (l[i-1] + l[i-2] + cnt[i-1]*len[i-2]%MOD) % MOD;
r[i] = (r[i-1] + r[i-2] + cnt[i-2]*len[i-1]%MOD) % MOD;
res[i] = (res[i-1] + res[i-2] + r[i-2]*cnt[i-1]%MOD + l[i-1]*cnt[i-2]%MOD) % MOD;
}
}
int main()
{
init(201314);
int t, n, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("Case #%d: %I64d\n", cas++, res[n]);
}
return 0;
}