当前位置:网站首页>Write some DP
Write some DP
2022-07-29 07:33:00 【A little Yu】
Flowers 【noip2012】 Popularization group
Title Description
Xiaoming's florist is newly opened , To attract customers , He wants to put a row of flowers in front of the flower shop , common m basin . By investigating customer preferences , Xiao Ming listed the customers' favorite n grow flowers , from 1 To n label . To show more flowers at the door , Rule No i You can't plant more than ai basin , When arranging flowers, put the same kind of flowers together , And different kinds of flowers need to be arranged in order from small to large .
Trial programming calculation , How many different flower arrangements are there .
Input format
The first line contains two positive integers n and m, Separated by a space .
The second line has n It's an integer , Every two integers are separated by a space , Sequential representation a1,⋯,an.
Output format
An integer , Indicates how many options there are . Be careful : Because the number of schemes may be many , Please output the number of scheme pairs 10^6+7 Results of die taking .
I/o sample
Input #1 Copy
2 4 3 2Output #1 Copy
2explain / Tips
【 Data range 】
about 20\%20% data , Yes 0<n \le 8,0<m \le 8,0 \le a_i \le 80<n≤8,0<m≤8,0≤ai≤8.
about 50\%50% data , Yes 0<n \le 20,0<m \le 20,0 \le a_i \le 200<n≤20,0<m≤20,0≤ai≤20.
about 100\%100% data , Yes 0<n \le 100,0<m \le 100,0 \le a_i \le 1000<n≤100,0<m≤100,0≤ai≤100.
NOIP 2012 Popularization group Third question
1. Search for
#include<bits/stdc++.h>
using namespace std;
const int maxn=105, mod = 1000007;
int n, m, a[maxn];
int dfs(int x,int k)
{
if(k > m) return 0;
if(k == m) return 1;
if(x == n+1) return 0;
int ans = 0;
for(int i=0; i<=a[x]; i++) ans = (ans + dfs(x+1, k+i))%mod;
return ans;
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
cout<<dfs(1,0)<<endl;
return 0;
}
2. Memory search
#include<bits/stdc++.h>
using namespace std;
const int maxn=105, mod = 1000007;
int n, m, a[maxn], rmb[maxn][maxn];
int dfs(int x,int k)
{
if(k > m) return 0;
if(k == m) return 1;
if(x == n+1) return 0;
if(rmb[x][k]) return rmb[x][k]; // Search and return
int ans = 0;
for(int i=0; i<=a[x]; i++) ans = (ans + dfs(x+1, k+i))%mod;
rmb[x][k] = ans; // Record the results of the current state
return ans;
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
cout<<dfs(1,0)<<endl;
return 0;
}
3. Two dimensional dynamic programming
#include<bits/stdc++.h>
using namespace std;
const int maxn=105, mod = 1000007;
int n, m, a[maxn], f[maxn][maxn];
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
f[0][0] = 1;
for(int i=1; i<=n; i++)
for(int j=0; j<=m; j++)
for(int k=0; k<=min(j, a[i]); k++)
f[i][j] = (f[i][j] + f[i-1][j-k])%mod;
cout<<f[n][m]<<endl;
return 0;
}
4. One dimensional dynamic programming
#include<bits/stdc++.h>
using namespace std;
const int maxn=105, mod = 1000007;
int n, m, a[maxn], f[maxn];
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
f[0] = 1;
for(int i=1; i<=n; i++)
for(int j=m; j>=0; j--) // Be careful , yes 01 knapsack
for(int k=1; k<=min(a[i], j); k++)
f[j] = (f[j] + f[j-k])%mod;
cout<<f[m]<<endl;
return 0;
}
边栏推荐
- Introduction to log4j layout
- zip gzip tar压缩进阶版
- Prometheus and grafana
- 【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
- Can the subset of the array accumulate K
- 1 - background project construction
- 【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK
- 分析25个主要DeFi协议的路线图 预见DeFi未来的七大趋势
- logback中RollingFileAppender属性简介说明
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
猜你喜欢
Sqlmap(SQL注入自动化工具)
一篇长文---深入理解synchronized
Thinkphp6 realizes database backup
亚马逊云助手小程序来啦!
国内数字藏品的乱象与未来
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
多线程购物
美智光电IPO被终止:年营收9.26亿 何享健为实控人
halcon的安装以及在vs2017中测试,vs2017中dll的配置
cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数
随机推荐
Use custom annotations to verify the size of the list
mysql 使用 DATE_FORMAT(date,'%Y-%m')
7-2 calculate the area and perimeter of a regular pentagon (25 points)
I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
STM32 operation w25q256 w25q16 SPI flash
How to establish EDI connection with Scania in Scania?
Segger's hardware anomaly analysis
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
QT topic: basic components (button class, layout class, output class, input class, container class)
Introduction to logback appender
Using C language to skillfully realize the chess game -- Sanzi chess
CDC source can quit after reading MySQL snapshot split
【暑期每日一题】洛谷 P6336 [COCI2007-2008#2] BIJELE
Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
[MySQL] - [subquery]
SEGGER 的硬件异常 分析
Levelfilter introduction
性能更佳、使用更简单的懒加载IntersectionObserverEntry(观察者)
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
Use of gcc/g++