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 @   DeepJay  阅读(99)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示