Secret Project Gym - 101972I (組合數+找規律)
There are n students working on a secret project, this project is very important and unique, so they decided to keep it safe, and protect it from leakage.
The students will put all the project's documents in a cabinet that has x locks, and each student will take a set of y
Each of the cabinet's lock has an infinite number of keys that can open it, and students may have different or similar sets of keys. In order to open the cabinet, z
Your task is to find minimum values of x and y. More formally, your task is to find what is the minimum number of locks needed and what is the minimum number of keys to the locks each student must carry such that the cabinet can be opened if and only if m
Input
The first line contains an integer T (1 ≤ T ≤ 105) specifying the number of test cases.
Each test case consists of a single line containing two integers n and m (1 ≤ m ≤ n ≤ 105), in which n is the number of students, and m is the minimum number of students that must be present to open the cabinet.
Output
For each test case, print a single line containing two integers x and y, in which xis the smallest number of locks needed, and y is the smallest number of keys to the locks each student must carry. Both numbers must be printed modulo 109 + 7.
Example
Input
4 3 2 2 2 5 4 5 3
Output
3 2 2 1 10 4 10 6
題意:一個櫃子上有x層鎖,一層鎖需要一把鑰匙,每個人至少拿一把鑰匙,問:至少去m個人才能把鎖開啟時,最少的鎖的數量x,和每個人拿的最少的鑰匙數y。
思路:多寫幾組資料+找找規律+大膽猜測=AC
比如:
3 2 鎖的數量x=C(3,2-1) 最少的鑰匙數y=C(3-1,2-1);
2 2 鎖的數量x=C(2,2-1) 最少的鑰匙數y=C(2-1,2-1);
5 4 鎖的數量x=C(5,4-1) 最少的鑰匙數y=C(5-1,4-1);
5 3 鎖的數量x=C(5,3-1) 最少的鑰匙數y=C(5-1,3-1);
因此:結果就是C(n,m-1),C(n-1,m-1);
如果對於組合數不太清楚的朋友,可以點選這裡https://blog.csdn.net/Daxian911/article/details/85037267
程式碼如下:
#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn=1e5+5; const int INF=0x3f3f3f3f; const int mod=1e9+7; ll n,m; ll fac[maxn]; ll ni[maxn]; ll ksm(ll a,ll b) { ll ans=1; while(b) { if(b&1)ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans; } void init(int n) { fac[0]=1; ni[0]=1; for(int i=1;i<=n;i++) { fac[i]=fac[i-1]*i%mod; } ni[n]=ksm(fac[n],mod-2); for(int i=n-1;i>=1;i--) { ni[i]=ni[i+1]*(i+1)%mod; } } ll C(ll n,ll m) { return fac[n]*ni[m]%mod*ni[n-m]%mod; } int main() { int T; init(100000); scanf("%d",&T); while(T--) { scanf("%lld%lld",&n,&m); printf("%lld %lld\n",C(n,m-1),C(n-1,m-1)); } return 0; }