ACwing 1214. 波动数列 (构造+dp)

题目链接:传送门

题目思路:由于初值是不定的,因此很难直接去求01背包,况且s范围是 1e-9 ~ 1e9; 

#include<bits/stdc++.h>
/*
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<cctype>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
*/
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef pair<double,double> pdd;
const int N=2e3+5;
const int M=8e5+5;
const int inf=0x3f3f3f3f;
const LL mod=1e8+7;
const double eps=1e-5;
const long double pi=acos(-1.0L);
#define ls (i<<1)
#define rs (i<<1|1)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mem(a,b) memset(a,b,sizeof(a))
LL read()
{
    LL x=0,t=1;
    char ch;
    while(!isdigit(ch=getchar())) if(ch=='-') t=-1;
    while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
    return x*t;
}

int main()
{
    //这到题不存在首项的具体值,如果直接构造 i-1 -> i ,dp是不能初始化的;
    //因此要寻找其他等价关系,通常可以给首项设置一个未知数,然后通过同余关系确定答案。
    LL n=read(),s=read(),a=read(),b=read();
    dp[0][0]=1;
    for(int i=1;i<n;i++)
        for(int j=0;j<n;j++)
            dp[i][j]=(dp[i-1][((j-a*(n-i))%n+n)%n]+dp[i-1][((j+b*(n-i)%n)%n+n)%n])%mod;
    //printf("%lld\n",s%n);
    printf("%lld\n",dp[n-1][(s%n+n)%n]);
    return 0;
}
View Code

 

posted @ 2020-10-15 20:27  DeepJay  阅读(95)  评论(0编辑  收藏  举报