[luoguP1005] 矩阵取数游戏(DP + 高精度)
和奶牛那个题很像,每一行状态互不影响,也就是求 n 遍DP
不过高精度非常恶心,第一次写,调了我一上午。
——代码
#include <cstdio>
#include <cstring>
#include <iostream> struct Big_int
{
int s[], idx;
Big_int()
{
idx = ;
memset(s, , sizeof(s));
}
}; int n, m;
Big_int ans, a[], f[][]; inline void clear(Big_int &x)
{
x.idx = ;
memset(x.s, , sizeof(x.s));
} inline Big_int Big(int x)
{
Big_int ret;
while(x)
{
ret.s[ret.idx] = x % ;
x /= ;
ret.idx++;
}
return ret;
} inline bool operator > (const Big_int x, const Big_int y)
{
int i;
if(x.idx > y.idx) return ;
else if(x.idx < y.idx) return ;
else for(i = x.idx - ; i >= ; i--)
if(x.s[i] > y.s[i]) return ;
else if(x.s[i] < y.s[i]) return ;
} inline Big_int Max(const Big_int x, const Big_int y)
{
return x > y ? x : y;
} inline int max(int x, int y)
{
return x > y ? x : y;
} inline Big_int operator + (const Big_int x, const Big_int y)
{
int i;
Big_int ret;
ret.idx = max(x.idx, y.idx) + ;
for(i = ; i < ret.idx; i++)
{
ret.s[i] += x.s[i] + y.s[i];
ret.s[i + ] += ret.s[i] / ;
ret.s[i] %= ;
}
while(!ret.s[ret.idx - ] && ret.idx > ) ret.idx--;
return ret;
} inline Big_int operator * (const Big_int x, const Big_int y)
{
int i, j;
Big_int ret;
ret.idx = x.idx + y.idx;
for(i = ; i < x.idx; i++)
for(j = ; j < y.idx; j++)
{
ret.s[i + j] += x.s[i] * y.s[j];
ret.s[i + j + ] += ret.s[i + j] / ;
ret.s[i + j] %= ;
}
while(!ret.s[ret.idx - ] && ret.idx > ) ret.idx--;
return ret;
} inline void print(const Big_int x)
{
int i;
if(!x.idx) printf("");
else for(i = x.idx - ; i >= ; i--) printf("%d", x.s[i]);
puts("");
} inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline Big_int dp(int x, int y, Big_int z)
{
if(f[x][y].idx) return f[x][y];
if(x == y) return f[x][y] = a[x] * z;
else return f[x][y] = Max(dp(x + , y, z * Big()) + a[x] * z, dp(x, y - , z * Big()) + a[y] * z);
} int main()
{
int i, j;
n = read();
m = read();
while(n--)
{
for(i = ; i <= m; i++) a[i] = Big(read());
for(i = ; i <= m; i++)
for(j = ; j <= m; j++)
clear(f[i][j]);
ans = ans + dp(, m, Big());
}
print(ans);
return ;
}
[luoguP1005] 矩阵取数游戏(DP + 高精度)的更多相关文章
- [LuoguP1005]矩阵取数游戏 (DP+高精度)
题面 传送门:https://www.luogu.org/problemnew/show/P1005 Solution 我们可以先考虑贪心 我们每一次都选左右两边尽可能小的数,方便大的放在后面 听起来 ...
- 【Luogu】P1005矩阵取数游戏(高精度+DP)
题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...
- [P1005][NOIP2007] 矩阵取数游戏 (DP+高精)
我不会高精…… 也不会DP…… 这道题即考高精又考DP…… 我要死了 给一个不是高精的代码(当然不能满分) #include<cstdio> #include<iostream> ...
- 1166 矩阵取数游戏[区间dp+高精度]
1166 矩阵取数游戏 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description [ ...
- P1005 矩阵取数游戏 区间dp 高精度
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n \times mn×m的矩阵,矩阵中的每个元素a_{i,j}ai,j均为非负整数.游戏规则如下: 每次取数时须从每行各取走一个元素,共n ...
- P1005 矩阵取数游戏[区间dp]
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的\(m*n\)的矩阵,矩阵中的每个元素\(a_{i,j}\)均为非负整数.游戏规则如下: 每次取数时须从每行各取走一个元素,共n个.经过m次后 ...
- 矩阵取数游戏 2007年NOIP全国联赛提高组(dp+高精)
矩阵取数游戏 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description [问题描述]帅帅经常跟 ...
- 矩阵取数游戏 NOIP 2007
2016-05-31 17:26:45 题目链接: NOIP 2007 矩阵取数游戏(Codevs) 题目大意: 给定一个矩阵,每次在每一行的行首或者行尾取一个数乘上2^次数,求取完最多获得的分数 解 ...
- 矩阵取数游戏洛谷p1005
题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...
随机推荐
- url中传参数为中文的转码与解码解决方法
1.转码 中文为 “你好” var ProjectName = encodeURI(encodeURI("你好"));,如下图所示 跳转页面 window.location.h ...
- PostgreSQL逻辑复制之pglogical篇
PostgreSQL逻辑复制之slony篇 一.pglogical介绍 pglogical 是 PostgreSQL 的拓展模块, 为 PostgreSQL 数据库提供了逻辑流复制发布和订阅的功能. ...
- 讯搜问题排查xunsearch
mysql导入数据不成功,开始重建索引后提示 [XSException] ../local/xunsearch/sdk/php/lib/XS.php(1898): DB- 可打印的版本 开始重建索引 ...
- hdu5924Mr. Frog’s Problem
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- E - A^B mod C (大数乘方取模)
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63) ...
- ACM_01背包2
背包4 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过W的 ...
- ACM_Uppercase(水题)
Uppercase Time Limit: 2000/1000ms (Java/Others) Problem Description: 给出一句中间无特殊符号的英语句子,要求将各单词的首字母改大写. ...
- WinForm ListBox 控件用法
下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...
- 【[转】MySql模糊查询
转自:http://chenpeng.info/html/2020 MySQL提供标准的SQL模式匹配,以及一种基于象Unix实用程序如vi.grep和sed的扩展正则表达式模式匹配的格式. 一.SQ ...
- 华硕(ASUS)X554LP笔记本重装win7后网卡和USB驱动问题的解决
以前在其它笔记本上采用U盘克隆安装winxp系统非常顺利,各种硬件驱动能自动识别并安装. 手上有一台别人的华硕(ASUS)X554LP笔记本,原装win8.1,用不惯,想装个win7旗舰版. 照例去系 ...