UVALive - 6801 Sequence

题目链接

https://icpcarchive.ecs.baylor.edu/external/68/6801.pdf

借下队友的代码。

#include<iostream>
#include<cstdio>
#include<cstring>
unsigned long long dp[1010][1010];
bool flag[1010][1010];
char a[1010];
using namespace std;
int main()
{
	int T;
	cin>>T;
	int t=1;
	while(T--)
	{
		unsigned long long n,k;
		cin>>n>>k;
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(a[i]=='1')
			{
				cnt++;
			}
		}
		memset(dp,0,sizeof(dp));
		memset(dp,false,sizeof(flag));
		dp[0][cnt]=1;
		flag[0][cnt]=true;
		if(k<cnt||(cnt-k)%2)
		{
			cout<<"Case #"<<t++<<": "<<0<<endl;
		}
		else
		{
			for(int i=1;i<=k;i++)
			{
				for(int j=0;j<=n;j++)
				{
					if(flag[i-1][j])
					{
						if(j+1<=n)
						{
							dp[i][j+1]=(dp[i-1][j]*(n-j)%1000000007+dp[i][j+1])%1000000007;
							flag[i][j+1]=true;
						}
						if(j-1>=0)
						{
							dp[i][j-1]=(dp[i-1][j]*j%1000000007+dp[i][j-1])%1000000007;
							flag[i][j-1]=true;
						}
					}
				}
			}
			cout<<"Case #"<<t++<<": "<<dp[k][0]<<endl;
		}
	}
	return 0;
}
时间: 2024-10-10 16:39:53

UVALive - 6801 Sequence的相关文章

找规律 UVALive 6506 Padovan Sequence

题目传送门 1 /* 2 找规律:看看前10项就能看出规律,打个表就行了.被lld坑了一次:( 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <iostream> 7 #include <cstring> 8 #include <cmath> 9 #include <string> 10 #include <vector> 11 #include &l

DP UVALive 6506 Padovan Sequence

题目传送门 /* 题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大 DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[i][j], dp[i/1-i][j-2] + a[i][j]); 要从前面一个转态推过来啊,我比赛写反了,内功不够:( */ #include <cstdio> #include <algorithm> #include <iostream> #include <

UVAlive 6763 Modified LCS

LCS stands for longest common subsequence, and it is a well known problem. A sequence in thisproblem means a list of integers, and a sequence X is considered a subsequence of another sequence Y ,when the sequence X can be obtained by deleting zero or

UVALive 5029 Encoded Barcodes

Encoded Barcodes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5029 Description All the big malls need a powerful system for the products retrieval. Now you are employed design a sub-system: rea

UVALive - 2965 Jurassic Remains

Jurassic Remains Time Limit: 18000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Paleontologists in Siberia have recently found a number of fragments of Jurassic period dinosaur skeleton. The pal

UVALive - 4108 SKYLINE[线段树]

UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Description The skyline of Singapore as viewed from the Marina Promenade (shown on the left) is one of the iconic scenes of Singapore. Country X would als

UVaLive 5031 Graph and Queries (Treap)

Graph and Queries Description You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it at the beginning. You’re also given a sequence of operations and you need to process them as r

UVALive 4764 简单dp水题(也可以暴力求解)

B - Bing it Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4764 Description I guess most of you played cards on the trip to Harbin, but I'm sure you have never played the following card game. Thi

UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】

Road Networks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4262 Description There is a road network comprised by M<tex2html_verbatim_mark> roads and N<tex2html_verbatim_mark> cities.