poj 3181 Dollar Dayz dp 完全背包
2015-11-07 21:54
330 查看
题目
题目链接:http://poj.org/problem?id=3181题目来源:《挑战》练习题
简要题意:用1⋯K1\cdots K面额的硬币有多少种方法构成NN元。
数据范围:1⩽N⩽1000;1⩽K⩽1001\leqslant N\leqslant 1000;\quad 1 \leqslant K \leqslant 100
题解
很裸的完全背包,不过直接搞会爆LL。java水过就行了。
之后发现极限数据就是二十几位吧,然后就顺手写了发c++的。
java代码
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger []a = new BigInteger[1005]; public static void main(String []args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(), k = cin.nextInt(); for (int i = 0; i <= n; i++) { a[i] = BigInteger.ONE; } for (int i = 2; i <= k; i++) { for (int j = i; j <= n; j++) { a[j] = a[j].add(a[j-i]); } } System.out.println(a ); cin.close(); } }
c++代码
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <queue> #include <string> #include <vector> #include <set> #include <map> #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define fi first #define se second using namespace std; typedef long long LL; typedef vector<int> VI; typedef pair<int,int> PII; LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;} // head struct LLL { static const LL MOD = 1e18; LL a[2]; inline LLL operator +=(const LLL &o) { a[0] += o.a[0]; a[1] += o.a[1]; if (a[0] >= MOD) a[1]++, a[0] -= MOD; } void wt() { if (a[1] > 0) printf("%I64d%018I64d\n", a[1], a[0]); else printf("%I64d\n", a[0]); } }; LLL dp[1005]; int main() { int n, k; scanf("%d%d", &n, &k); dp[0].a[0] = 1; for (int i = 1; i <= k; i++) { for (int j = i; j <= n; j++) { dp[j] += dp[j-i]; } } dp .wt(); return 0; }
相关文章推荐
- 单链表的的逆置(带头结点)
- 线程1: i++; i++; 线程2: i+=2; 的问题 (2013年某公司笔试题目)
- C#中WebBrowser控件的版本问题
- 从分布式一致性谈到CAP理论、BASE理论
- CodeForces 567E President and Roads(最短路 + 双联通分量)
- python统计《悲惨世界》中出现次数最多的20个单词并绘制柱状图
- python socket模块学习
- 排序算法(五):冒泡排序
- java学习收藏
- 文本管理命令
- 编译安装 php5.6
- android 5.0新特性CardView教程
- 基于PhalApi的DB集群拓展 V0.1bate
- AVAudioRecorder录音和AVAudioPlayer播放
- iOS设置applicationIconBadgeNumber和消息推送
- python之函数用法globals()
- java性能监控工具jmc-windows
- 磁珠的作用
- WordPress 自定义搜索框
- phalapi-进阶篇2(DI依赖注入和单例模式)