HDU 1028 Ignatius and the Princess III
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028
整数拆分
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 155;
typedef __int64 INT;
INT c[maxn],tmp_c[maxn];
int main(){
int n;
while( cin >> n ){
for(int i = 0;i <= n;++i) c[i] = 1;
for(int i = 2;i <= n;++i){
for(int k = 0;k <= n;k += i){
for(int j = 0;j + k <= n;++j) tmp_c[k + j] += c[j];
}
for(int i = 0;i <= n;++i){
c[i] = tmp_c[i];
tmp_c[i] = 0;
}
}
printf("%I64d\n",c[n]);
}
return 0;
}
HDU 1398 Square Coins
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1398
整数拆分成平方数的和
#include <bits/stdc++.h>
using namespace std;
const int maxn = 350;
typedef __int64 INT;
INT c[maxn],tmp_c[maxn];
int main(){
int n;
while( cin >> n && n ){
for(int i = 0;i <= n;++i) c[i] = 1;
for(int i = 2;i*i <= n;++i){
for(int k = 0;k <= n;k += i*i){
for(int j = 0;j + k <= n;++j) tmp_c[k + j] += c[j];
}
for(int i = 0;i <= n;++i){
c[i] = tmp_c[i];
tmp_c[i] = 0;
}
}
printf("%I64d\n",c[n]);
}
return 0;
}
HDU 1085 Holding Bin-Laden Captive!
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085
求最小的不能组成的数
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
typedef unsigned long long UINT;
UINT C[maxn],tmp_C[maxn];
int main(){
int a,b,c;
while( cin >> a >> b >> c ){
if(!a && !b && !c) break;
int MAX = a,ans = -1;
memset(C,0,sizeof(C));
memset(tmp_C,0,sizeof(tmp_C));
for(int i = 0;i <= a;++i) C[i] = 1;
for(int i = 0,j = 0;j <= b;++j,i += 2){
for(int k = 0;k <= MAX;++k) tmp_C[i + k] += C[k];
}
MAX = a + b * 2;
for(int k = 0;k <= MAX;++k){
C[k] = tmp_C[k];
tmp_C[k] = 0;
}
for(int i = 0,j = 0;j <= c;++j,i += 5){
for(int k = 0;k <= MAX;++k) tmp_C[i + k] += C[k];
}
MAX = MAX + c * 5;
for(int k = 0;k <= MAX;++k){
if(!tmp_C[k]){
ans = k;
break;
}
}
if(ans == -1) ans = MAX + 1;
printf("%d\n",ans);
}
}
HDU 1171 Big Event in HDU
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171
将N个物品分成尽量等值的两堆
#include <bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 100000 + 10;
bool C[maxn],tmp_C[maxn];
int v[maxn],cnt[maxn];
int main(){
int n,MAX,tmp_max,ret;
while( scanf("%d",&n) ){
if(n < 0) break;
ret = 0;
for(int i = 0;i < n;++i) sf("%d %d",&v[i],&cnt[i]),ret += v[i] * cnt[i];
memset(C,0,sizeof(C)),memset(tmp_C,0,sizeof(tmp_C));
C[0] = 1;
MAX = tmp_max = 0;
for(int i = 0;i < n;++i){
for(int j = 0,k = 0;j <= ret / 2 && k <= cnt[i];++k,j += v[i]){
for(int p = 0;p <= MAX && p + j <= ret / 2;++p){
tmp_C[p + j] = tmp_C[p + j] | C[p];
tmp_max = max(tmp_max,p + j);
}
}
for(int j = 0;j <= tmp_max;++j){
C[j] = tmp_C[j];
tmp_C[j] = 0;
}
MAX = tmp_max;
}
pf("%d %d\n",ret - MAX,MAX);
}
return 0;
}
HDU 1709 The Balance
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1709
N个数可加可减也可不操作 输出不能组成的数
#include <bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 100 + 5 , maxm = 10000 + 5;
bool C[maxm * 2],tmp_C[maxm * 2];
int num[maxn];
int main(){
int n,ret,ans_rt,flag;
while( sf("%d",&n) != EOF ){
ret = ans_rt = 0;
memset(C,0,sizeof C),memset(tmp_C,0,sizeof tmp_C);
C[maxm] = 1,ret = 0;
for(int i = 0;i < n;++i) sf("%d",&num[i]),ret += num[i];
for(int i = 0;i < n;++i){
for(int j = maxm - ret;j <= ret + maxm;++j) tmp_C[j] = C[j];
for(int j = maxm - ret;j + num[i] <= ret + maxm;++j){
tmp_C[j + num[i]] = tmp_C[j + num[i]] | C[j];
}
for(int j = maxm - ret + num[i];j <= ret + maxm;++j){
tmp_C[j - num[i]] = tmp_C[j - num[i]] | C[j];
}
for(int j = maxm - ret;j <= ret + maxm;++j){
C[j] = tmp_C[j];
tmp_C[j] = 0;
}
}
for(int i = maxm;i <= ret + maxm;++i){
if(!C[i]) ans_rt++;
}
pf("%d\n",ans_rt);
flag = 0;
if(ans_rt) for(int i = maxm;i <= ret + maxm;++i){
if(!C[i]){
if(flag) pf(" %d",i - maxm);
else{
flag = 1;
pf("%d",i - maxm);
}
}
}
if(ans_rt) pf("\n");
}
return 0;
}
HDU 2069 Coin Change
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069
最多100 个 1,5,10,25,50问能组成N的方案数
#include <bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 255,maxm = 100 + 5;
const int MAX_CNT = 105;
int C[MAX_CNT][maxn],tmp_C[MAX_CNT][maxn];
int a[] = {5,10,25,50};
int main(){
memset(C,0,sizeof C);
memset(tmp_C,0,sizeof tmp_C);
for(int i = 0;i <= 100;++i) C[i][i] = 1;
for(int i = 0;i < 4;++i){
for(int CNT = 0;CNT <= 100;++CNT){
for(int j = 0;j < maxn;++j){
for(int c = 0,k = 0;c + CNT <= 100 && j + k < maxn;c++,k += a[i]){
tmp_C[CNT + c][j + k] += C[CNT][j];
}
}
}
for(int CNT = 0;CNT <= 100;++CNT){
for(int j = 0;j < maxn;++j){
C[CNT][j] = tmp_C[CNT][j];
tmp_C[CNT][j] = 0;
}
}
}
for(int i = 0;i < maxn;++i){
for(int CNT = 0;CNT < 100;++CNT) C[100][i] += C[CNT][i];
}
int n = -1;
while( sf("%d",&n) != EOF ) pf("%d\n",C[100][n]);
return 0;
}