Anya and Cubes - CodeForces 525 E dp
2015-04-16 21:45
323 查看
Anya and Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from
left to right, with natural numbers written on them. She also has kstickers with exclamation marks. We know that the number of stickers does not exceed
the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it
reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most kexclamation marks so that
the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can
you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
Input
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the
number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the
numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
Sample test(s)
input
output
input
output
input
output
题意:一共有n个数,你可以选择其中的一些数,然后最多使其中的k个数变成它的阶乘,问使其和为S共有多少种方案。
思路:dp思路很简单,但是如果是简单dp的话会TLE。解决方案是分两半,每一半做一个dp,由左一半的所有结果去找右一半的方案数。比如左边和为S2的方案数乘以右边和为S-S2的方案数,还要注意阶乘的使用次数不超过k。
AC代码如下:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from
left to right, with natural numbers written on them. She also has kstickers with exclamation marks. We know that the number of stickers does not exceed
the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it
reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most kexclamation marks so that
the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can
you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
Input
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the
number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the
numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
Sample test(s)
input
2 2 30 4 3
output
1
input
2 2 7 4 3
output
1
input
3 1 11 1 1
output
6
题意:一共有n个数,你可以选择其中的一些数,然后最多使其中的k个数变成它的阶乘,问使其和为S共有多少种方案。
思路:dp思路很简单,但是如果是简单dp的话会TLE。解决方案是分两半,每一半做一个dp,由左一半的所有结果去找右一半的方案数。比如左边和为S2的方案数乘以右边和为S-S2的方案数,还要注意阶乘的使用次数不超过k。
AC代码如下:
#include<cstdio> #include<cstring> #include<map> using namespace std; typedef long long ll; map<ll,ll> match[3][30]; map<ll,ll>::iterator iter; int n,m; ll S,jie[20],val[30]; void solve(int pos,int num,ll S2,ll k) { if(S2+k<=S) match[pos][num][S2+k]+=match[pos][num][S2]; if(num>0 && k<=18 && S2+jie[k]<=S) match[pos][num-1][S2+jie[k]]+=match[pos][num][S2]; } void dp(int pos,int l,int r) { int i,j,k,p; match[pos][m][0]=1; for(p=l;p<=r;p++) for(i=0;i<=m;i++) if(!match[pos][i].empty()) { iter=match[pos][i].end(); iter--; while(iter!=match[pos][i].begin()) { solve(pos,i,iter->first,val[p]); iter--; } solve(pos,i,iter->first,val[p]); } } int main() { int i,j,k; ll ans=0,ret,S2; jie[0]=1; for(i=1;i<=18;i++) jie[i]=jie[i-1]*i; scanf("%d%d%I64d",&n,&m,&S); for(i=1;i<=n;i++) scanf("%I64d",&val[i]); dp(1,1,n/2); dp(2,n/2+1,n); for(i=0;i<=m;i++) for(iter=match[1][i].begin();iter!=match[1][i].end();iter++) { S2=iter->first; ret=0; for(k=m;k>=m-i;k--) ret+=match[2][k][S-S2]; ans+=iter->second *ret; } printf("%I64d\n",ans); }
相关文章推荐
- codeforces 525E E. Anya and Cubes( dp+折中搜索)
- codeforces 297 E. Anya and Cubes
- codeforces 525 E Anya and Cubes 中途相遇法
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
- Arthur and Brackets - CodeForces 508 E dp贪心
- Codeforces Round #297 (Div. 2) E. Anya and Cubes
- Codeforces 508C - Anya and Ghosts (模拟 + 贪心)
- codeforces 525 D Arthur and Walls
- Anya and Ghosts - CodeForces 508 C 水题
- Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)
- 【Codeforces Round #297 (Div. 2)】Codeforces 525E Anya and Cubes
- Codeforces 525E Anya and Cubes
- codeforces--C - Anya and Ghosts(贪心)
- codeforces--C - Anya and Ghosts(贪心+模拟)
- 【CodeForces】508C - Anya and Ghosts(贪心 & 模拟)
- codeforces 508 C. Anya and Ghosts
- codeforces 525E Anya and Cubes(暴力,meet-in-the-middle)
- Codeforces 508C Anya and Ghosts
- Arthur and Walls - CodeForces 525 D 搜索
- 【codeforces 508C】Anya and Ghosts