24h購物| | PChome| 登入
2013-06-24 15:49:50| 人氣1,337| 回應0 | 上一篇 | 下一篇

[UVA] 10742 - The New Rule in Euphomia

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C
New Rule in Euphomia
Input: standard input
Output: standard output
Time Limit: 1 second

 

The ancient country of Euphomia had some strange rules regarding payment of prices and values of coins. When King Albert ruled this country the following payment rules were in place:

 

a)      The name of the currency was Abacus.

b)      Only coins were used to pay any certain amount.

c)      The coins had integer values. Eg. There is no coin with value 1.5 Abacus. And the amount to be paid was also always integer.

d)      There was no 1 Abacus coin.  

e)      Payments of any amount had to be made with coins of the same value. For example when one paid 9 Abacus he paid it with three 3-Abacus coins. 

 

So coins were made so that any one could pay any amount within 2-1000000 abacuses following the above rules. The coins, which were not required, were not made. For example there were no 6-Abacus coin because one could pay 6 Abacus as (3+3) Abacus. The problem with the above rule was that often one had to use many coins to pay a particular amounts. For example to pay the amount 2048 Abacus one had to use 1024 2-Abacus coins which is quite terrible. Therefore, when king Lucas captured this country he made unlimited coins of value 1 Abacus and added the following rule:

“One can only use exactly two different coins made during Albert's regime. In addition with these coins one can also use the 1-Abacus coins at any number. However one cannot use only the 1-Abacus coins to make payment."

 

So in this new rule paying 1 Abacus is still impossible but rule d) and e) of Albert’s regime is now obsolete. With this new rule in place your job is to find out in how many ways one could pay a certain amount n.

For example one can now pay 10-Abacus in the following five ways:

a)      3+2+1+1+1+1+1

b)      7+3

c)      5+2+1+1+1

d)      7+2+1

e)      5+3+1+1

Input

The input file contains less than 600 lines of inputs. Each line of the input file contains one value of n (0<n<=1000000), which indicates the amount to be paid. Input is terminated by a line where n=0. Obviously this line should not be processed.

Output

For each line of input except the last one produce one line of output, which contains the serial of output followed by an integer which indicates in how many ways one can pay n abacus with the new rules in place. Look at the output for sample input for details.

Sample Input                           Output for Sample Input

10

996542

24

129

0

Case 1: 5

Case 2: 1661327749

Case 3: 22

Case 4: 296

 


Problem setter: Shahriar Manzoor, EPS

Special thanks (for alternate solution): Derek Kisman, EPS

 



題目描述:

其實前面講了很多規則,但是最後又修成了,
確切要兩種不同幣值>1 的使用,而且可以用無數個 1 元硬幣。
而且硬幣的幣值會是質數

題目解法:

二分搜索可以匹配的另一個質數個數。



#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxL (1000000>>5)+1
#define SET(x) (mark[(x)>>5] |= 1<<((x)&31))
#define GET(x) (mark[(x)>>5]>>((x)&31)&1)
int mark[maxL];
int p[100000], pt = 0;
void sieve() {
    register int i, j, k;
    const int n = 1000000;
    SET(1);
    for(i = 2; i <= n; i++) {
        if(!GET(i)) {
            p[pt++] = i;
            for(k = n/i, j = i*k; k > 0; k--, j -= i)
                SET(j);
        }
    }
}
int main() {
    sieve();
    int cases = 0, n;
    while(scanf("%d", &n) == 1 && n) {
        long long ret = 0;
        int upp, i;
        for(i = 0; i < pt && p[i] < n; i++) {
            upp = upper_bound(p, p+pt, n-p[i]) - p;
            upp --;
            if(upp > i)
                ret += upp - i;
        }
        printf("Case %d: %lld\n", ++cases, ret);
    }
    return 0;
}

台長: Morris
人氣(1,337) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二分] 10567 - Helping Fill Bates
此分類上一篇:[UVA] 1241 - Jollybee Tournament

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文