列表

详情


NC24830. [USACO 2009 Feb S]Bulls and Cows

描述

Farmer John wants to position N (1 <= N <= 100,000) cows and bulls in a single row to present at the annual fair.
FJ has observed that the bulls have been quite pugnacious lately; if two bulls too close together in the line, they will argue and begin to fight, ruining the presentation. Ever resourceful, FJ calculated that any two bulls must have at least K (0 <= K < N) cows between them in order to avoid a fight.
FJ would like you to help him by counting the number of possible sequences of N bulls and cows that avoid any fighting. FJ considers all bulls the to be the same and all cows to be the same; thus, two sequences are only different if they have different kinds of cattle in some position.

输入描述

* Line 1: Two space-separated integers: N and K

输出描述

* Line 1: A single integer representing the number of ways FJ could create such a sequence of cattle. Since this number can be quite large, output the result modulo 5,000,011.

示例1

输入:

4 2

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 4ms, 内存消耗: 1940K, 提交时间: 2023-04-01 15:41:36

#include <bits/extc++.h>
#define int long long
using namespace std;
constexpr int p = 5000011;
int a[100002],b[100003];
void solve() {
    int n,k;cin>>n>>k;
    a[1]=b[1]=1;
    for(int i=2;i<=n;++i)
       {
            a[i]=a[i-1]+b[i-1];
            if(i-k-1>0)(b[i]=b[i-k-1]+a[i-k-1])%=p;
        else b[i]=1;
        }
    cout<<(a[n]+b[n])%p;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t = 1;
    // std::cin >> t;
    for (; t--; solve());
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 760K, 提交时间: 2020-01-22 09:50:31

#include<cstdio>
using namespace std;
const int mod=5000011;
int f[100005],n,k;
int main()
{
	scanf("%d%d",&n,&k);
	f[0]=1;k++;
	for(int i=1;i<=n;i++)
	{
		f[i]=f[i-1];
		if(i<=k)f[i]++;
		else f[i]+=f[i-k];
		f[i]%=mod;
	}
	printf("%d",f[n]);
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 732K, 提交时间: 2019-09-14 08:27:39

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n, k;
	cin >> n >> k;
	vector <int> f(n + 1);
	auto F = [&] (int x) {return x < 1 ? 1 : f[x];};
	for (int i = 1; i <= n; ++i)
		f[i] = (F(i - 1) + F(i - k - 1)) % 5000011;
	cout << f[n];
}

上一题