当前位置:网站首页>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;
}边栏推荐
- I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test
- @Detailed explanation of requestmapping usage
- 写点dp
- CDC source can quit after reading MySQL snapshot split
- 5-integrate swagger2
- What is the function of fileappender in logback?
- Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
- How to establish EDI connection with Scania in Scania?
- Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
- 7-2 calculate the area and perimeter of a regular pentagon (25 points)
猜你喜欢
随机推荐
Does Flink support sqlserver databases? Get the changes of SQLSERVER database
What is the function of fileappender in logback?
cdc source能读完MySqlSnapshotSplit 就退出嘛
Thinkphp6 realizes database backup
Halcon installation and testing in vs2017, DLL configuration in vs2017
5-integrate swagger2
如何与斯堪尼亚SCANIA建立EDI连接?
Paper reading (62):pointer networks
stm32 操作W25Q256 W25Q16 spi flash
Vue router route cache
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
Introduction to logback appender
Amazon cloud assistant applet is coming!
Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
MySQL uses the client and select methods to view the summary of blob type fields
Description of rollingfileappender attribute in logback
Synchronous / asynchronous, blocking / non blocking and IO
力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
国内数字藏品的乱象与未来

![【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna](/img/9a/f857538c574fb54bc1accb737d7aec.png)







