http://codeforces.com/problemset/problem/474/D
思路:F[i]=F[i-1]+(i>=K)F[i-k]
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 const int Mod=1000000007; 7 int K,sum[200005],f[200005]; 8 int read(){ 9 int t=0,f=1;char ch=getchar(); 10 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 11 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 12 return t*f; 13 } 14 void init(){ 15 f[0]=1; 16 for (int i=1;i<=100000;i++){ 17 f[i]=(f[i-1])%Mod; 18 if (i>=K) 19 f[i]=(f[i]+f[i-K])%Mod; 20 } 21 for (int i=1;i<=100000;i++) 22 sum[i]=(sum[i-1]+f[i])%Mod; 23 } 24 int main(){ 25 int T=read();K=read(); 26 init(); 27 while (T--){ 28 int l=read(),r=read(); 29 printf("%d ",((sum[r]-sum[l-1])%Mod+Mod)%Mod); 30 } 31 return 0; 32 }