sequence2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 90

Problem Description
Given an integer array bi with a length of n, please tell me how many exactly different increasing subsequences.
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.
 
Input
Several test cases(about 5)
For each cases, first come 2 integers, n,k(1≤n≤100,1≤k≤n)
Then follows n integers ai(0≤ai≤109)
 
Output
For each cases, please output an integer in a line as the answer.
 
Sample Input
3 2
1 2 2
3 2
1 2 3
 
Sample Output
2
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)的更多相关文章

  1. Hdu 5568 sequence2 高精度 dp

    sequence2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=556 ...

  2. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  3. POJ 1625 Censored!(AC自动机+高精度+dp)

    http://poj.org/problem?id=1625 题意: 给出一些单词,求长度为m的串不包含这些单词的个数. 思路: 这道题和HDU 2243和POJ 2778是一样的,不同的是这道题不取 ...

  4. 【Luogu】P1005矩阵取数游戏(高精度+DP)

    题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...

  5. POJ 1737 Connected Graph(高精度+DP递推)

    题面 \(solution:\) 首先做个推销:带负数的压位高精度(加减乘+读写) 然后:由 \(N\) 个节点组成的无向图的总数为: \(2^{N*(N-1)/2}\) (也就是说这个图总共有 \( ...

  6. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  7. HDU 5568:sequence2 大数+DP

    sequence2  Accepts: 93  Submissions: 358  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  8. TYVJ 矩阵取数 Label:高精度+dp

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  9. HDU 5568 sequence2 区间dp+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...

随机推荐

  1. 如何使用 Barracuda 防火墙设置/保护 Azure 应用程序

     如果某企业在 Windows Azure 上托管某个应用程序,该应用程序会在某个特定时间暴露到 Internet,以用于商业用途.公共 Internet 带来 客户的同时也带来了攻击者. Tim ...

  2. mysql下的SELECT INTO语句

    在mysql下使用SELECT INTO语句会产生ERROR 1327 (42000): Undeclared variable:new_tablename 此时要使用: CREATE TABLE C ...

  3. Three Swaps DFS

    E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Break the Chocolate(规律)

    Break the Chocolate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. IT忍者神龟之Struts2.xml配置全然正确流程能走通可是有红叉解决

    一:Multiple annotations found at this line:Undefined actionName  parameter  Undefined actionnamespace ...

  6. Angular JS 学习笔记(一)

    1. 菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 2. Angular JS中文网:http://www.apjs.net ...

  7. 基本属性 - iOS中的本地通知

    本地通知的基本使用 创建本地通知 设置属性 调度通知(添加通知到本地通知调度池) 注册用户通知权限(只需一次, 可以单独放在Appdelegate中, 或者别的地方) —> iOS8以后必须, ...

  8. BZOJ 1800: [Ahoi2009]fly 飞行棋( 枚举 )

    O(N2)算出有x条直径然后答案就是x(x-1)/2...这个数据范围是闹哪样! ----------------------------------------------------------- ...

  9. zookeeper 安装

    Zookeeper安装 一.   下载zookeeper http://www.apache.org/dist/zookeeper/stable/ 二.   解压zookeeper.tar >& ...

  10. 爬虫框架Scrapy

    前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下. 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点. 于是乎,爬虫 ...