HDU 1502 Regular Words(大数+dp)
来源:互联网 发布:mac系统word下载地址 编辑:程序博客网 时间:2024/05/18 13:31
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1502
思路:dp[3*n][n][n][n],第一维是长度,第2.3.4维是a,b,c的个数,写出的转移方程不是超Int就是超内存,java跑出的结果和别人AC代码跑出的结果相同,思路是对的
后来参考别人的思路又想了一个省空间的状态转移方程dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1],java的捕异常让人很烦啊
AC代码:
import java.io.*;import java.util.*;import java.math.BigInteger;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);int n;while (cin.hasNextInt()) {n = cin.nextInt();BigInteger[][][] dp = new BigInteger[61][61][61];for(int i=0; i<=60; i++){for(int j=0; j<=60; j++){for(int k=0; k<=60; k++){dp[i][j][k] = BigInteger.ZERO;}}}dp[1][0][0] = BigInteger.ONE;dp[1][1][0] = BigInteger.ONE;dp[1][1][1] = BigInteger.ONE;for(int i=2; i<=n; i++){for(int j=0; j<=n; j++){if(j > i)continue;for(int k=0; k<=n; k++){if(k > j)continue;dp[i][j][k] = dp[i][j][k].add(dp[i-1][j][k]);if(j - 1 < i && j - 1 >= 0)dp[i][j][k] = dp[i][j][k].add(dp[i][j-1][k]);if(k - 1 < j && k - 1 >= 0)dp[i][j][k] = dp[i][j][k].add(dp[i][j][k-1]);}}}System.out.println(dp[n][n][n]+"");System.out.println("");}}}
错误代码1:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int dp[200][60][60][60];int main(){ int n; while(scanf("%d",&n) != EOF) { memset(dp,0,sizeof(dp)); dp[1][1][0][0] = 1; for(int i=2; i<=3*n; i++) { for(int j=1; j<=n; j++) { for(int k=0; k<=n; k++) { if(k > j) continue; for(int l=0; l<=n; l++) { if(l > k) continue; if(j + k + l != i-1) continue; if(j + 1 <= n) dp[i][j+1][k][l] += dp[i-1][j][k][l]; if(k < j && k + 1 <= n) dp[i][j][k+1][l] += dp[i-1][j][k][l]; if(l < k && l + 1 <= n) dp[i][j][k][l+1] += dp[i-1][j][k][l]; } } } } printf("%d\n\n",dp[3*n][n][n][n]); } return 0;}直接超了int,改用Java写
错误代码2:
import java.io.*;import java.util.*;import java.math.BigInteger;public class Test4 {public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);int n;while(cin.hasNextInt()){n = cin.nextInt();BigInteger[][][][] dp = new BigInteger[181][61][61][61];for(int i=1; i<=180; i++){for(int j=0; j<=60; j++){for(int k=0; k<=60; k++){if(k > j)continue;for(int l=0; l<=60; l++){if(l > k)continue;dp[i][j][k][l] = BigInteger.ZERO;}}}}dp[1][1][0][0] = BigInteger.ONE;for(int i=2; i<=3*n; i++){for(int j=1; j<=n; j++){for(int k=0; k<=n; k++){if(k > j)continue;for(int l=0; l<=n; l++){if(l > k)continue;if(j + k + l != i-1)continue;if(j+1 <=n)dp[i][j+1][k][l] = dp[i][j+1][k][l].add(dp[i-1][j][k][l]);if(k < j && k+1 <= n)dp[i][j][k+1][l] = dp[i][j][k+1][l].add(dp[i-1][j][k][l]);if(l < k && l+1 <= n)dp[i][j][k][l+1] = dp[i][j][k][l+1].add(dp[i-1][j][k][l]);}}}}System.out.println(dp[3*n][n][n][n]+"");System.out.print("");}}}
超内存了
0 0
- HDU 1502 Regular Words(大数+dp)
- hdu 1502 Regular Words(java大数加法+dp)
- HDU 1502 Regular Words(DP + JAVA)
- 【DP】HDU-1502 Regular Words
- hdu1502 Regular Words(树型dp+大数)
- HDU1502 Regular Words(DP+大数模拟)
- HDU1502:Regular Words(大数DP)
- Hdu 1502 Regular Words (java高精度 + DP)
- HDU 1502 Regular Words 打表+大数相加 .
- hdu1502 Regular Words--DP&大数问题
- hdu 1502 Regular Words
- hdu 1502 Regular Words
- hdu 1502 Regular Words
- hdu 1502 Regular Words DP + 高精度 第一个java
- HDU 1502 Regular Words(打表预处理dp)
- [HDU 1502][动态规划]Regular Words
- hdu1502 Regular Words 【dp+java】
- [练习][HDU1502]DP Regular Words
- windows平台下 文件备份批处理和邮件发送批处理
- AndroidStudio之NDK开发流程(手动版)
- Mysql与Oracle区别
- datagridview的增删改查 并且带有刷新
- AFNetWorking实现代码延迟
- HDU 1502 Regular Words(大数+dp)
- 表单验证<AngularJs>
- AR捡取纹理功能实现 colARmix&涂涂乐
- 重装python idle打不开解决方案
- 【译文】如何用R生成回归结果Word报告
- Mysql与Oracle区别
- ubuntu 忘记用户名或密码的重置方法
- 微信公众平台开发(三) 订阅事件(subscribe)处理
- iOS会遇到的面试题(一)