[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. ...
随机推荐
- EntityFramework:An error occurred while executing the command definition. See the inner exception for details.
错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...
- spring Cache /Redis 缓存 + Spring 的集成示例
spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...
- [C++ STL] 常用算法总结
1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...
- 【知识总结】多项式全家桶(三)(任意模数NTT)
经过两个月的咕咕,"多项式全家桶" 系列终于迎来了第三期--(雾) 上一篇:[知识总结]多项式全家桶(二)(ln和exp) 先膜拜(伏地膜)大恐龙的博客:任意模数 NTT (在页面 ...
- LN : leetcode 529 Minesweeper
lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...
- JavaScript(十)基本包装类
基本包装类都具有对象的基本方法 toString 和 valueOf Number 数字是原始类型,那为啥还有方法? 因为他在执行方法的时候会创建一个对应的包装类对象,这个对象有这种方法, ...
- 微信小程序组件解读和分析:二、scroll-view可滚动视图区域
scroll-view可滚动视图区域组件说明: 可滚动视图区域. 组件用法:纵向滚动用法 Tip: 使用竖向滚动时,需要给一个固定高度,通过 WXSS 设置 height,否则无法滚动. 当滚动到顶部 ...
- APP上线被APPStore拒绝的各种原因
1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3.游戏里有实物奖励的话,一定要说清楚,奖励由本公司负责,和苹果没有关系.4.用到苹果的标志 ...
- Concurrent control in SQLite
This document describes the technologies to concurrent access to a SQLite database. There are also s ...
- glic,uClibc,EGLIBC 简要介绍
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...