腾讯算法笔试
白花问题,哪里出错了啊,调了一个多小时通过0
long int mod = 1e9 + 7;
int main() {
int n;
int k;
cin >> n;
cin >> k;
for (int i = 0; i < n; i++)
{
int a;
int b;
cin >> a >> b;
if (k>b)
{
cout << 1 << endl;
continue;
}
vector<long int> dp(b + 1,0);
for (int j = 1; j < k; j++)
dp[j] = 1;
dp[k] = 2;
for (int j = k + 1; j <= b; j++)
{
dp[j] = (dp[j - 1] + dp[j - k]) % mod;
}
long int num = 0;
for (int j = a; j <= b; j++)
{
num = (num + dp[j])%mod;
}
cout << num << endl;
}
}
#腾讯##笔试题目#int main() {
int n;
int k;
cin >> n;
cin >> k;
for (int i = 0; i < n; i++)
{
int a;
int b;
cin >> a >> b;
if (k>b)
{
cout << 1 << endl;
continue;
}
vector<long int> dp(b + 1,0);
for (int j = 1; j < k; j++)
dp[j] = 1;
dp[k] = 2;
for (int j = k + 1; j <= b; j++)
{
dp[j] = (dp[j - 1] + dp[j - k]) % mod;
}
long int num = 0;
for (int j = a; j <= b; j++)
{
num = (num + dp[j])%mod;
}
cout << num << endl;
}
}