# Enigmatic Partition

## 题目描述：

• a i a_i 是整数， 1 ≤ a i ≤ n 1 \le a_i \le n f o r for 1 ≤ i ≤ m 1 \leq i \leq m ，并且
• a i ≤ a i + 1 ≤ a i + 1 a_i \leq a_{i + 1} \leq a_i + 1 表示 1 ≤ i ≤ m 1 \leq i \leq m ，并且
• a m = a 1 + 2 a_m = a_1 + 2

f ( n ) f(n) n n 的神秘分区的数量。 给定 l l r r ，请计算 ∑ i = l r f ( i ) \sum_ {i = l} ^ r f(i)

3
5 7
7 9
1 9

Case #1: 2
Case #2: 7
Case #3: 8

## 说明：

f(1) = 0.
f(2) = 0.
f(3) = 0.
f(4) = 0.
f(5) = 0.
f(6) = 1: 6 = 1 + 2 + 3.
f(7) = 1: 7 = 1 + 1 + 2 + 3.
f(8) = 2: 8 = 1 + 1+ 1 + 2 + 3, 8 = 1 + 2 + 2 + 3.
f(9) = 4: 9 = 1 + 1 + 1 + 1 + 2 + 3, 9 = 1 + 1 + 2 + 2 + 3, 9 = 1 + 2 + 3 + 3, 9 = 2 + 3 + 4

## 思路：

• a 1 + a 2 + a 3 = m a_1+a_2+a_3=m
• a 1 ， a 2 ， a 3 ≥ 1 a_1，a_2，a_3 \ge 1

• a 1 v a l + a 2 ( v a l + 1 ) + a 3 ( v a l + 2 ) a_1val+a_2(val+1)+a_3(val+2)
• = a 1 v a l + a 2 v a l + a 2 + a 3 v a l + 2 a 3 =a_1val+a_2val+a_2+a_3val+2a_3
• = v a l ( a 1 + a 2 + a 3 ) + a 2 + 2 a 3 =val(a_1+a_2+a_3)+a_2+2a_3
• = v a l m + a 2 + 2 a 3 =valm+a_2+2a_3
• = n =n

• 二阶差分

• 隔项差分

v a l m + a 2 + 2 a 3 = n valm+a_2+2a_3=n

n9101112131415161718
a 3 = 4 a_3=4 123333
a 3 = 3 a_3=3 112333122333
a 3 = 2 a_3=2 111233112233122233
a 3 = 1 a_3=1 111123111223112223122223

f n : f_n: 1122211000

## A C AC C o d e Code :

#include<bits/stdc++.h>
#define ll long long
const int MAXN=1e5+10;
ll t,l,r,qian[MAXN<<1],f[MAXN<<2];
int main(){
for(int i=3;i<MAXN;++i)
for(int j=i;j<MAXN;j+=i){
f[j+3]++;
f[j+2*i]++;
f[j+i+1]--;
f[j+i+2]--;
}
for(int i=3;i<MAXN;++i) f[i]+=f[i-2];
for(int i=1;i<MAXN;++i){
f[i]+=f[i-1];
qian[i]=qian[i-1]+f[i];
}
scanf("%lld",&t);
for(int Case=1;Case<=t;Case++){
scanf("%lld%lld",&l,&r);
printf("Case #%d: %lld\n",Case,qian[r]-qian[l-1]);
}
}

08-04 868
06-09 6万+
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试