博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #302 (Div. 1)
阅读量:5093 次
发布时间:2019-06-13

本文共 5190 字,大约阅读时间需要 17 分钟。

转载请注明出处:           ——by fraud

 

A. Writing Code

Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes.

Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v2 + ... + vn = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v1 lines of the given task, then the second programmer writes v2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most b bugs in total.

Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.

Input

The first line contains four integers nmbmod (1 ≤ n, m ≤ 500, 0 ≤ b ≤ 500; 1 ≤ mod ≤ 109 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

The next line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of bugs per line for each programmer.

Output

Print a single integer — the answer to the problem modulo mod.

Sample test(s)
input
3 3 3 100 1 1 1
output
10
input
3 6 5 1000000007 1 2 3
output
0
input
3 5 6 11 1 2 1
output
0

一个完全背包的问题,cf的3秒,果断上n^3dp

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 using namespace std;22 #define XINF INT_MAX23 #define INF 0x3FFFFFFF24 #define MP(X,Y) make_pair(X,Y)25 #define PB(X) push_back(X)26 #define REP(X,N) for(int X=0;X
=L;X--)29 #define CLR(A,X) memset(A,X,sizeof(A))30 #define IT iterator31 typedef long long ll;32 typedef pair
PII;33 typedef vector
VII;34 typedef vector
VI;35 ll dp[510][510];36 ll a[510];37 int main()38 {39 ios::sync_with_stdio(false);40 int n,m,b,MOD;41 cin>>n>>m>>b>>MOD;42 for(int i=1;i<=n;i++){43 cin>>a[i];44 }45 dp[0][0]=1;46 for(int i=1;i<=n;i++){47 for(int j=1;j<=m;j++){48 for(int k=0;k<=b;k++){49 if(k>=a[i])50 dp[j][k]+=dp[j-1][k-a[i]];51 dp[j][k]%=MOD;52 }53 }54 55 }56 ll ans=0;57 for(int i=0;i<=b;i++){58 ans+=dp[m][i];59 ans%=MOD;60 }61 cout<
<

 

D. Road Improvement

The country has n cities and n - 1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.

All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city x to any other city contains at most one bad road.

Your task is — for every possible x determine the number of ways of improving the quality of some roads in order to meet the citizens' condition. As those values can be rather large, you need to print each value modulo 1 000 000 007 (109 + 7).

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 2·105) — the number of cities in the country. Next line contains n - 1positive integers p2, p3, p4, ..., pn (1 ≤ pi ≤ i - 1) — the description of the roads in the country. Number pi means that the country has a road connecting city pi and city i.

Output

Print n integers a1, a2, ..., an, where ai is the sought number of ways to improve the quality of the roads modulo 1 000 000 007 (109 + 7), if the capital of the country is at city number i.

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

树形dp

一开始看完D题,一想,这不是树形DP水题嘛,中间用个除法,逆元搞一搞就好了。。。然后就被petr给hack了。。。原因便是会发生除0的情况,于是,我们需要避免出现除法。

那么我们需要统计出不包括这个子树的,并且以其父亲为根的方案数。

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 using namespace std;22 #define XINF INT_MAX23 #define INF 0x3FFFFFFF24 #define MP(X,Y) make_pair(X,Y)25 #define PB(X) push_back(X)26 #define REP(X,N) for(int X=0;X
=L;X--)29 #define CLR(A,X) memset(A,X,sizeof(A))30 #define IT iterator31 typedef long long ll;32 typedef pair
PII;33 typedef vector
VII;34 typedef vector
VI;35 const ll MOD=1000000007;36 ll dp[200010];37 ll dp1[200010];38 vector
G[200010];39 ll dp2[200010];40 ll pre[200010];41 ll last[200010];42 void dfs(int x){43 dp[x]=1;44 for(int i=0;i
=0;i--){59 int v = G[x][i];60 last[i]=last[i+1]*(dp[v]+1)%MOD;61 }62 for(int i=0;i
>n;77 int a;78 for(int i=2;i<=n;i++){79 cin>>a;80 G[a].PB(i);81 }82 fill(dp2,dp2+n+2,1);83 dfs(1);84 dfs1(1);85 for(int i=1;i<=n;i++)cout<
<<" ";86 cout<

 

转载于:https://www.cnblogs.com/fraud/p/4486632.html

你可能感兴趣的文章
java之hibernate之helloworld
查看>>
微服务之初了解(一)
查看>>
Iterator invalidation(迭代器失效)
查看>>
GDOI DAY1游记
查看>>
RHEL 无图形界面安装oracle 11gr2
查看>>
sql连接left join、right join、inner join的使用
查看>>
h5 的localStorage和sessionStorage存到缓存里面的值是string类型
查看>>
自定义序列化
查看>>
1020. 分解质因数
查看>>
linux下的shell——如何修改shell的提示符,能够出现登录用户名、主机名和路径...
查看>>
网络流量监测图形分析工具 Cacti
查看>>
php session 和cookie
查看>>
Java中的小知识。
查看>>
如何执行超过一百兆(100MB)的sql脚本?
查看>>
git merge的recursive策略和merge-base
查看>>
JS创建对象的几种方式
查看>>
python:实例化configparser模块读写配置文件
查看>>
博客首发
查看>>
redis源码分析之发布订阅(pub/sub)
查看>>
理解flexbox(一)
查看>>