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结尾的长度为 ...
随机推荐
- AES 加密
package com.sprucetec.tms.utils; import java.security.Key; import javax.crypto.Cipher;import javax.c ...
- zip文件压缩(转)
zip文件结构 上面中的每一行都是一个条目,zip文件就是由一个或者多个条目组成. 条目在Java中对应ZipEntry类 创建zip压缩文件 ...
- android基础篇------------java基础(12)(多线程操作)
<一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...
- 设计模式的C++实现 2.工厂模式
工厂模式,实例化对象,用工厂方法取代new操作. 工厂模式基本与简单工厂模式差点儿相同,简单工厂中每次加入一个子类必须在工厂类中加入一个推断分支,这违背了开闭原则.而工厂模式的解决方法是将简单工厂中的 ...
- PE框架学习之道:PE框架——style的配置
1.在style.xml中定义style <style id="NumberStyle"> <setting> <param name=&qu ...
- 原生JS实现字符串分割
window.onload = function(){ var str = 'abc,dbc,qqq,aaa'; var sp = split(str,',')//与字符串的分隔符要一直. alert ...
- 简单JSONP跨域请求
JSONP原理:利用<script>标签的src属性实现跨域的请求.可在URL中提供回调函数的名字.后台进过处理后将数据以回调函数参数的形式返回. demo:JSONP请求不同端口的数据 ...
- IntelliJ IDEA 14 注册码生成器
IntelliJ IDEA 14 注册码生成器 文件为Java代码 自己编译运行里面的程序输入名称然后就生成注册码了工具:http://yun.baidu.com/s/1cZKsA部分工具生成的注册码 ...
- spring和mybatis整合进行事务管理
1.声明式实现事务管理 XML命名空间定义,定义用于事务支持的tx命名空间和AOP支持的aop命名空间: <beans xmlns="http://www.springframewor ...
- Windows最常用的几个网络CMD命令总结
Windows最常用的几个网络CMD命令总结 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 一.ping 主要是测试本机T ...