SPOJ Problem 346:Bytelandian gold coins

有一种价值n的硬币能换成n/2,n/3,n/4的三个硬币,要求硬币价值的和尽可能多。

前十万打表,后面就BFS。。。

#include<cstdio>
#include<cstring>
int a[100005];
int q[100005],l,r,t,i,n;
long long ans;
int main(){
    for (i=0;i<=100000;i++){
        a[i]=a[i/2]+a[i/3]+a[i/4];
        if (i>a[i])a[i]=i;
    }
    while(scanf("%d",&n)!=EOF){
        l=0;r=1;ans=0;
        memset(q,0,sizeof(q));
        q[1]=n;
        while(l<r){
            t=q[++l];
            if (t<=100000)ans+=a[t];
            else {
                q[++r]=t/2;
                q[++r]=t/3;
                q[++r]=t/4;
            }
        }
        printf("%lld\n",ans);
    }
}
时间: 2024-11-08 10:16:11

SPOJ Problem 346:Bytelandian gold coins的相关文章

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)

Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the

hust 1170 - Baskets of Gold Coins

题目描述 You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes

H - Gold Coins(2.4.1)

H - Gold Coins(2.4.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of

Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1862    Accepted Submission(s): 1108 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

[ACM] POJ 2000 Gold Coins

Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 13098 Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of the next two days

HDU2401 Baskets of Gold Coins【水题】【推理】

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1648    Accepted Submission(s): 966 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

hdoj 2401 Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 1104 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

Gold Coins(闲来无事水一发)

Gold Coins Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21155 Accepted: 13265 Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of the next two days (th