Relax! It's just a game(排列組合,簡單)
Relax! It's just a game
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
You: What's the score? Did I miss much?
Me: It's 2-1 for elAhli and the second half just started. The first half was quite boring.
You: Who scored first? elAhli or ezZamalek?
Me: What difference does it make?
You: Big difference! I can predict the outcome of the match if I knew the order of which goals were scored in the first half.
Me: What do you mean?
You: It's 2-1 for elAhli, right? One of three things could have happened: elAhli scored two goals then ezZamalek scored; Or, elAhli scored its first goal, then ezZamalek, then elAhli again; Or, ezZamalek scored first, then elAhli scored its two goals.
Me: So?!! I still don't understand what difference does that make? It's still 2-1 for elAhli! Why don't you just relax and let us continue watching the game in peace.
You: You don't understand!! I believe theprobability of who'll win depends on the order of how goals were scored. Now I have to predict the outcome for 3 possibilities.
Me: And what if the score was 3-2? What would you have done then?
You: I would have to work for 5 different possibilities. No?
Me: Of course not! The number of possibilities isn't always equal to the sum.
You: Can you tell me when will it be equal to the sum?
Me: You're a programmer, why don't you write a program that counts the number of possibilities and compare it to the sum?
You: I don't have the time, I want to watch the match. Besides, I have nine other problems to worry about.
Me: I'll give you a hint. The possibilities will be equal to the sum only if one of the teams scored a certain number of goals.
Input
Your program will be tested on one or more test cases. Each test case specifies two natural numbers (A and B) (separated by one or more spaces) representing the score of the first half. No team will be able to score more than 10 goals. The last line of the input file contains two -1's (which is not part of the test cases.)
Output
Format For each test case where the number of possibilities is equal to the sum, print:
A+B=C
Where A and B are as above and
C is their sum. If the number of possibilities is not equal to the sum, replace the `=' sign with `!=' (without the quotes.)
Sample Input
2 1 1 0 -1 -1
Sample Output
2+1=3 1+0=1
思路:進球總數為a+b,容易想到所有排列情況為(a + b)!,但注意同一個隊伍所進的球在排列中不應當視為互異,比如甲隊進2個球:a1,a2,則a1a2和a2a1是同一種排列,於是所有排列情況數應為:(a + b)! / (a ! * b!),所以可能的情況數等於兩隊進球總和時有:
(a + b)! / (a ! * b!)= a + b,即是(a + b - 1)! / (a ! * b!) = 1……(1)。.利用這個關係就能判斷對於特定的a和b,情況數和a+b的關係。注意題目中給的提示“The possibilities will be equal to the sum only if one of the teams scored a certain number of goals”,化簡(1),可以得到,a、b中最小值為1,也就是說,a和b至少有一個為1。
還需要注意的是,a = b = 0時,情況數不等於進球總數,這個時候情況數為1.
AC CODE:
//Memory: 0 KB Time: 12 MS
//Language: C++ 4.1.2 Result: Accepted
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
long long fact(long long n)
{
if(n == 1 || n == 0) return 1;
else return n * fact(n - 1);
}
int main()
{
long long a, b;
int idx;
double p;
string str[2] = {"=", "!="};
while(cin >> a >> b && a != -1 && b != -1)
{
idx = 1;
if(a+b)
{
p = 1.0 * fact(a + b - 1) / fact(a) / fact(b);
if(fabs(p - 1) <= 1e-8)
idx = 0;
}
cout << a << "+" << b << str[idx] << a + b << endl;
}
return 0;
}
相關文章
- 排列組合
- 【數學】組合數學 - 排列組合
- 組合數學筆記-排列與組合筆記
- 字串排列組合問題字串
- js運算元組中資料排列組合JS
- 無重複字串的排列組合字串
- It's not just a place to restREST
- 【POJ 2249】 Binomial Showdown 組合數學 排列組合計算
- 常見規格排列組合問題
- Just as it's the EA name attached to it
- 【原創】開源.NET排列組合元件KwCombinatorics使用(一)—組合生成元件
- k14s - 遵循Unix哲學的簡單、可組合的Kubernetes工具
- acm-排列組合學習筆記(更新中)ACM筆記
- 遞迴演算法實踐---實現排列組合遞迴演算法
- 程式設計師必備演算法——排列組合程式設計師演算法
- Just for fun——PHP框架之簡單的路由器(2)PHP框架路由器
- 回溯問題Python框架總結——排列組合問題Python框架
- Python使用combinations實現排列組合Python
- CF1796C C. Maximum Set 題解 排列組合
- Eclipse+Resin=簡單易用的web開發組合EclipseWeb
- python:用pyinstaller做個排列組合的小工具Python
- 增補部落格 第十六篇 python 排列組合序列Python
- 遞迴示例-指定數字以內的所有排列組合(Reduce)遞迴
- 遊戲創新的一般方法論,本質就是排列組合?遊戲
- 生成{1,2,...,n}的排列的演算法-組合數學演算法
- 【R語言學習筆記】探索ggplot的排列組合(一)R語言筆記
- 筆試小技巧--隔板法解排列組合問題(附程式碼)筆試
- IOS開發基礎—簡單的qq表情排列appiOSAPP
- 極簡設計模式-組合模式設計模式
- 簡單瞭解組策略
- JavaScript 引擎和 Just-in-Time 編譯概念,Hot Function 的簡單介紹JavaScript編譯Function
- 在 SAP 電商雲 Spartacus UI 裡手動注入 module 的幾種排列組合UI
- 一對一聊天原始碼,驗證碼生成隨機數字排列組合原始碼隨機
- leetcode 面試題08.08. 有重複字串的排列組合LeetCode面試題字串
- 【R語言學習筆記】探索ggplot的排列組合:線圖(一)R語言筆記
- 組合語言--單步中斷組合語言
- HDU 4944 FSF’s game(計數遊戲)GAM遊戲
- 簡單介紹python輸出列表元素的所有排列形式Python