Codeforces 1335A Candies and Two Sisters 糖果與兩姊妹

難易度 : 800

英文原文如下

There are two sisters Alice and Betty. You have n candies. You want to distribute these n candies between two sisters in such a way that:

  • Alice will get a (a>0) candies;
  • Betty will get b (b>0) candies;
  • each sister will get some integer number of candies;
  • Alice will get a greater amount of candies than Betty (i.e. a>b);
  • all the candies will be given to one of two sisters (i.e. a+b=n).

Your task is to calculate the number of ways to distribute exactly n candies between sisters in a way described above. Candies are indistinguishable.

Formally, find the number of ways to represent n as the sum of n=a+b, where a and b are positive integers and a>b.

You have to answer independent test cases.

中文翻譯

有兩個姊妹 Alice 跟 Betty,現在你有 n 個糖果,你希望將這 n 個糖果按照以下方式分給兩姊妹:

  • Alice 會得到  a (a>0) 顆糖果
  • Betty 會得到  b (b>0) 顆糖果
  • 姊妹都會得到某個整數顆糖果
  • Alice 會比Betty得到更多糖果 (a>b)
  • 所有的糖果都會被給予其中一個人 (a+b=n)

你的任務是要計算有多少種方式來分配 n 顆糖果給這對姊妹(依照上面的規則)

具體地說,找到將 n 表示為 n=a+b 的方法數,其中 a 和 b 是正整數,且 a>b。

你需要回答 t 個獨立的測試案例。

輸入

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of a test case contains one integer n (1≤n≤2⋅109) — the number of candies you have.

第一行輸入包含了一個整數 t (1≤t≤104) — 測試案例的數量,接下去有 t 個案例。

案例一行中包含了一個整數 n (1≤n≤2⋅109) — 你/妳有的糖果數量。

輸出

For each test case, print the answer — the number of ways to distribute exactly n candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print 0.

對每一個測試案例,印出答案 — 有多少種方法可以依照題目規則來分配  n 顆糖果給兩姊妹。要是沒有任何方法來滿足所有條件,則回傳 0

範例

輸入6
7
1
2
3
2000000000
763243547
輸出3
0
0
1
999999999
381621773

筆記

For the test case of the example, the 3 possible ways to distribute candies are:

針對範例中的測試案例(1),有三個可能的方式來分配糖果

  • a=6, b=1;
  • a=5, b=2;
  • a=4, b=3.

解題思路

看完題目之後,先來整理一下邏輯

首先兩姊妹都必須至少有一顆糖果,且Alice又必須比Betty多,故最少應至少為3顆 (要是小於的話,也就表示沒辦法分配了)

根據以上,可以得到的是,Betty可獲得糖果的範圍介於 1 到 n/2 之間(不包含)

C# 解決方案

方案1

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    int tmp = int.Parse(Console.ReadLine());
    
    if(tmp<3){
        Console.WriteLine(0);
    }
    else{
        Console.WriteLine(tmp%2==0 ? tmp/2-1 : tmp/2);
    }
}

要是輸入是偶數,則必須再減1 (ex: 400/2 = 200, 200-1 就是Betty能獲得的最多糖果數,也是共有幾種分配方法)

要是奇數的話就不用另外減1,因為整數相除時已經無條件捨去尾數了

Java 解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < n; i++) 
        {
            int tmp = scanner.nextInt();
            scanner.nextLine();
            
            if(tmp<3){
                System.out.println(0);
            }
            else{
                System.out.println(tmp%2==0 ? tmp/2-1 : tmp/2);
            }
        }
    }
}

結論

分配有效的資源永遠都讓人感到困擾呢~

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持 (幫我多多分享也大感謝!!)

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 1335A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *