1. 程式人生 > >Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online

Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online

  • Problem Description:
    Connecting the display screen and signal sources which produce different color signals by cables, then the display screen can show the color of the signal source.Notice that every signal source can only send signals to one display screen each time.
    Now you have M display screens and K different signal sources(K≤M≤2^32−1). Select K display screens from M display screens, how many cables are needed at least so that any

    K display screens you select can show exactly K different colors.

  • Input
    Multiple cases (no more than 100), for each test case:
    there is one line contains two integers M and K.

  • Output
    Output the minimum number of cables N.

  • Sample Input
    3 2
    20 15

  • Sample Output
    4
    90

  • Hint
    這裡寫圖片描述
    As the picture is shown, when you select M1 and M2, M1 show the color of K1, and M2 show the color of K2.
    When you select M3 and M2, M2 show the color of K1 and M3 show the color of K2.
    When you select M1 and M3, M1 show the color of K1.

  • 題目分析:對於每個測試樣例給兩個整數,分別是M個display screens,K個different signal sources,(K≤M≤2^32−1),從M個screens中選出K個screens,在源頭和螢幕之間連線,使得這K個螢幕可以顯示出不同的K中顏色。
    連線只存在源頭和螢幕之間。

  • 我的思路:
    首先,對於K個源點,先對K個螢幕連一條線,以K=3,M=4為例子:
    這裡寫圖片描述
    每個源點選擇一個螢幕進行連線,如下圖:
    這裡寫圖片描述
    這樣會剩下(M-K)個螢幕在連結的時候一定會連結不同的源點,並且連結的源點一定是K個,因為在顯示的時候是要求選擇K個螢幕同時出現不同的顏色,如果連結的源點少於K個就會出現重複顏色。如下圖:
    這裡寫圖片描述


    最後,可以得到一個公式:
    需要的連線數目=K+(M-K)* K , 整理得K*(M+1)-K*K
    需要注意一下M,K的範圍,需要用long long存,要不就是WA…..Orz

  • 完整程式碼:
#include<stdio.h>
int main(void)
{
    long long M, K;
    while (scanf("%lld%lld", &M, &K) != EOF)
    {
        printf("%lld\n", K*(M + 1) - K*K);
    }
    return 0;
}