藍橋杯題目練習 [藍橋杯]波動數列

[藍橋杯]波動數列

題目描述

觀察這個數列:1 3 0 2 -1 1 -2 …這個數列中後一項總是比前一項增加2或者減少3。
棟棟對這種數列很好奇,他想知道長度爲 n 和爲 s 而且後一項總是比前一項增加a或者減少b的整數數列可能有多少種呢?

輸入

輸入的第一行包含四個整數 n s a b,含義如前面說述。
1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000。

輸出

輸出一行,包含一個整數,表示滿足條件的方案數。由於這個數很大,請輸出方案數除以100000007的餘數。

樣例輸入 Copy

4 10 2 3

樣例輸出 Copy

2

來自大佬的代碼:https://www.cnblogs.com/imzscilovecode/p/8666783.html

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
 
using namespace std;
 
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1000000+10;
const int mod = 100000000+7;
ll dp[500000+10];
int main(){
    ll n,s,a,b;
    cin>>n>>s>>a>>b;
    dp[0]=1;
    for(int i=1;i<n;i++){
        for(int j=i*(i+1)/2;j>=i;j--){
            dp[j]=(dp[j-i]+dp[j])%mod;
        }
    }
    ll div=(n-1)*n/2;
    ll sum=s+div*b;
    ll dir=a+b;
    ll ans=0;
    for(int i=0;i<=div;i++){
        if(sum%n==0){
            ans=(ans+dp[i])%mod;
        }
        sum-=dir;
    }
    cout<<ans;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章