Codeforces Round #266 (Div. 2)D(DP)

本文介绍了一个利用动态规划求解序列等值问题的方法。通过定义状态转移方程,作者详细解释了如何计算使得序列中所有元素等于目标值h的不同方式数量,并提供了求解过程中的关键步骤和代码实现。
摘要由CSDN通过智能技术生成
D. Increase Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of "adding one on the segment [l, r]": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.

How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.

Input

The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)
input
3 2
1 1 1
output
4
input
5 1
1 1 1 1 1
output
1
input
4 3
3 2 1 1
output
0

题意:RT

思路:DP[i][j]表示到第i个位置,前面还有j个点未被选(不包括i)

            转移有两种情况

           1. a[i]+j==h  表示可以从前面j个点中任意选一个点与i匹配或者不选
 
           2. a[i]+j+1==h 表示可以从前面j个点以及i中任意选一个点与后面的点匹配或者不选

           具体看代码吧~~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;

typedef long long ll;
const int MOD = 1000000007;
const int MAXN = 2010;
int dp[MAXN][MAXN];
int a[MAXN];
int n,h;

int dfs(int i,int k)
{
    if(dp[i][k])return dp[i][k];
    if(i==n)return dp[i][k]= k==0 ?  1 : 0;
    int res=0;
    if(a[i]+k==h)res=((ll)dfs(i+1,k-1)*k % MOD+dfs(i+1,k)) % MOD;
    else if(a[i]+k+1==h)res=((ll)dfs(i+1,k)*(k+1) % MOD + dfs(i+1,k+1) ) %MOD;
    return dp[i][k]=res;
}

int main()
{
    scanf("%d%d",&n,&h);
    for(int i=0;i
       
       
         h){ printf("0\n"); return 0; } } printf("%d\n",dfs(0,0)); return 0; } 
       
      
      
     
     
    
    
   
   
### 关于 Codeforces Round 997 Div. 2 的题目及解析 #### A. XOR Mixup 在这个问题中,给定了两个整数 \(a\) 和 \(b\) ,以及一个正整数 \(k\) 。目标是在不超过 \(k\) 步内通过交换 \(a\) 和 \(b\) 中任意一位来使得两者相等。如果可以在指定步数内完成,则返回 "YES";否则返回 "NO"[^1]。 对于这个问题的一个有效解决方案是计算不同位的数量并判断其是否小于等于两倍的 k 值加上 a 和 b 的二进制表示中最右边不同的位置索引之差。这是因为每一步最多能改变一对不匹配的位置状态。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long a, b, k; cin >> a >> b >> k; bitset<32> ba(a), bb(b); int diff = 0; for(int i=0; i<32; ++i){ if(ba[i]!=bb[i])++diff; } cout << ((abs(__builtin_ctzll(a ^ b)) + 2 * k >= diff) ? "YES\n":"NO\n"); } } ``` #### B. Array Shrinking 此题描述了一个数组缩小的过程:允许选取连续子数组并将它们替换为其最大公约数值(GCD),直到整个数组变成单个元素为止。询问最终剩余的那个唯一数字是什么样的最小可能值[^2]? 解决方法涉及到动态规划的思想——维护一个二维表 dp[][],其中dp\[l\]\[r\] 表达的是区间 \([l,r]\) 能够被压缩成的最大 GCD 数字。转移方程基于枚举中间点 m 来分割原区间为更小子区间的组合方式实现更新。 ```cpp const int N = 2e5+7; long long gcd(long long a,long long b){return !b?a:gcd(b,a%b);} vector<int> v(N); unordered_map<long long,int> mp[N]; void solve(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i)v[i]=rand()%N+1; memset(mp,0,sizeof(mp)); for(int len=1;len<=n;++len) for(int l=1;l+len-1<=n;++l){ int r=l+len-1; if(len==1)mp[l][v[l]]=1; else{ unordered_set<long long> st; for(auto &p : mp[l]) if(p.second>=len-1&&gcd(v[r],p.first)==v[r]){ printf("0");exit(0); }else{st.insert(gcd(v[r],p.first));} for(auto x:st)mp[l][x]++; } } puts(to_string(mp[1].begin()->first).c_str()); } signed main(){solve();} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值