sequence2(高精度dp)
sequence2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 220 Accepted Submission(s): 90
P.S. A subsequence bai(1≤i≤k) is an increasing subsequence of sequence bi(1≤i≤n) if and only if 1≤a1<a2<...<ak≤n and ba1<ba2<...<bak. Two sequences ai and bi is exactly different if and only if there exist at least one i and ai≠bi.
For each cases, first come 2 integers, n,k(1≤n≤100,1≤k≤n)
Then follows n integers ai(0≤ai≤109)
1 2 2
3 2
1 2 3
3
题解:让求一个数列中长度为k的LIS数列的种数(指的数组下标);所以想到用dp,二维dp,dp[i][j]其中i指的是长度,j指的是以j结束的数;所以可以列出状态转移方程;
dp[x][i]=dp[x-1][j]+dp[x][i];每当if(m[i]>m[j])时 开始从2到n遍历;由于数量太大,所以要用到高精度。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
struct BIGINT{
int num[],len;
void init(int x){
mem(this->num,);
this->num[]=x;
this->len=;
}
};
BIGINT operator + (BIGINT a,BIGINT b){
BIGINT c;
c.init();//c要记得初始化。。。
int len=max(a.len,b.len);
for(int i=;i<len;i++){
c.num[i]=a.num[i]+b.num[i]+c.num[i];
if(c.num[i]>1e8)c.num[i]-=1e8,c.num[i+]++;
if(c.num[len])len++;
}c.len=len;
return c;
}
void print(BIGINT a){
for(int i=a.len-;i>=;i--){
printf("%d",a.num[i]);
}puts("");
}
BIGINT dp[][],ans;//以j结尾长度为i的个数
int m[];
int main(){
int n,k;
while(~scanf("%d%d",&n,&k)){
for(int i=;i<=n;i++)scanf("%d",m+i);
mem(dp,);
for(int i=;i<=n;i++)dp[][i].init();
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
if(m[i]>m[j]){
for(int x=;x<=n;x++){
dp[x][i]=dp[x-][j]+dp[x][i];
}
}
ans.init();
for(int i=;i<=n;i++)ans=ans+dp[k][i];
print(ans);
}
return ;
}
大神优化过的代码。。。好难懂。。。还没懂。。。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ull unsigned long long
#define ll long long
#define N 100005
#define BASE 13131
int n,k;
int a[105];
struct Cbig
{
int num[20],len;
void init(int x)
{
len=1;
num[0]=x;
}
}dp[2][105],ans,c;
Cbig add(Cbig &a,Cbig &b)
{
c.len=max(a.len,b.len);
c.num[0]=0;
for(int i=0;i<c.len;i++)
{
c.num[i+1]=0;
if(i<a.len) c.num[i]+=a.num[i];
if(i<b.len) c.num[i]+=b.num[i];
if(c.num[i]>=100000000)
{
c.num[i]-=100000000;
c.num[i+1]=1;
}
}
if(c.num[c.len]) c.len++;
return c;
}
void print(Cbig &a)
{
printf("%d",c.num[a.len-1]);
for(int i=a.len-2;i>=0;i--)
printf("%08d",a.num[i]);
puts("");
}
int main()
{
//freopen("tt.in", "r", stdin);
while(cin>>n>>k)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dp[0][0].init(1);
for(int i=1;i<=n;i++) dp[0][i].init(0);
int p=0,q=1;
for(int t=1;t<=k;t++)
{
p^=1;q^=1;
for(int i=0;i<=n;i++) dp[p][i].init(0);
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
if(j==0||a[j]<a[i])
dp[p][i]=add(dp[p][i],dp[q][j]);
}
}
ans.init(0);
for(int i=1;i<=n;i++)
ans=add(ans,dp[p][i]);
print(ans);
}
return 0;
}
sequence2(高精度dp)的更多相关文章
- Hdu 5568 sequence2 高精度 dp
sequence2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=556 ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- POJ 1625 Censored!(AC自动机+高精度+dp)
http://poj.org/problem?id=1625 题意: 给出一些单词,求长度为m的串不包含这些单词的个数. 思路: 这道题和HDU 2243和POJ 2778是一样的,不同的是这道题不取 ...
- 【Luogu】P1005矩阵取数游戏(高精度+DP)
题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...
- POJ 1737 Connected Graph(高精度+DP递推)
题面 \(solution:\) 首先做个推销:带负数的压位高精度(加减乘+读写) 然后:由 \(N\) 个节点组成的无向图的总数为: \(2^{N*(N-1)/2}\) (也就是说这个图总共有 \( ...
- POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)
题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...
- HDU 5568:sequence2 大数+DP
sequence2 Accepts: 93 Submissions: 358 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- TYVJ 矩阵取数 Label:高精度+dp
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
- HDU 5568 sequence2 区间dp+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...
随机推荐
- Jmeter接口測试
一.创建project.引包 1.创建JAVAproject 2.引入Jmeter中lib\ext基础包:ApacheJMeter_java.jar.ApacheJMeter_core.jar 3.引 ...
- iOS 基于Socket 的 C/S 网络通信结构(下一个)
以前实现简单 Server 程序,服务端通过 void WriteStreamClientCallBack(CFWriteStreamRef stream, CFStreamEventType eve ...
- MVC设计模式JavaWeb实现
JSP开发模式 jsp开发模式的发展 1.模式1:(适合小型项目的技术的开发) a.第一版本号.纯jsp(封装数据.处理数据,显示数据) b.第二版本号,Jsp+JavaBean. ...
- bulk insert data into database with table type .net
1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar]() NUL ...
- java线程池分析和应用
比较 在前面的一些文章里,我们已经讨论了手工创建和管理线程.在实际应用中我们有的时候也会经常听到线程池这个概念.在这里,我们可以先针对手工创建管理线程和通过线程池来管理做一个比较.通常,我们如果手工创 ...
- 使用RadioGroup与RadioButton实现多选一
RadioGroup是RadioButton的集合, RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个 RadioGroup的orientation(方向) ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- MacOS + Linux + Nginx
Asp.Net Core 发布和部署( MacOS + Linux + Nginx ) 前言 在上篇文章中,主要介绍了 Dotnet Core Run 命令,这篇文章主要是讲解如何在Linux中,对 ...
- OJ python答题结果"返回非零"
最近在OJ上用python答题,偶尔会遇到结果“放回非零”的情况(Non-zero Exit Code) 总结了以下,目前知道的是这些: 1. 在python2中用了input(),或在python3 ...
- java大作业 KShinglingAlgorithm
wiki上关于KShingling Algorithm(w-shingling)的说明: http://en.wikipedia.org/wiki/W-shingling 摘要: In natural ...