using System.Numerics;publicclassCheckChoose{publicstaticlongCheckchoose(long m,long n){
BigInteger[] factorialArr =newBigInteger[n +1];
factorialArr[0]=1;
factorialArr[1]=1;for(long i =2; i <= n; i++){
factorialArr[i]=(BigInteger)i * factorialArr[i -1];}for(long i =1; i <= n; i++){if(m == factorialArr[n]/(factorialArr[i]* factorialArr[n - i]))return i;}return-1;}}
答案2:
using System;publicclassCheckChoose{privatestaticlongChoose(int n,int p){if(p > n)return0;if(p > n - p) p = n - p;long ret =1;for(int i =0; i < p; i++){
ret *= n - i;
ret /= i +1;}return ret;}publicstaticlongCheckchoose(long m,int n){long mx;if(n %2==0) mx =Choose(n,(int)(n /2));else mx =Choose(n,(int)Math.Floor((double)(n /2)));if(m > mx){return-1;}int i =0;while(i <=(int)Math.Floor((double)(n /2))+1){if(Choose(n, i)== m){return i;}
i++;}return-1;}}
答案3:
using System.Numerics;publicclassCheckChoose{publicstaticlongCheckchoose(long m,int n){for(BigInteger i =1; i <= n /2; i++){var top =newBigInteger(1);for(BigInteger j = n; j > n - i; j--) top *= j;var bottom =newBigInteger(1);for(long j =1; j <= i; j++) bottom *= j;if(top / bottom == m)returnlong.Parse(i.ToString());if(top / bottom > m)break;}return-1;}}
答案4:
using System.Numerics;publicclassCheckChoose{publicstaticlongCheckchoose(long m,int n){BigInteger x =1;BigInteger r =1;for(int i =1; i<n/2+1; i++){
x *= n-i+1;
r *= i;if( x / r == m )return i;}return-1;}}
答案5:
usingstatic System.Math;publicclassCheckChoose{publicstaticlongCheckchoose(long m,int n){for(int k =1; k <= n; k++){if(Round(CalculateBinominalCoefficient(n, k))== m){return k;}}return-1;}publicstaticdoubleCalculateBinominalCoefficient(int n,int k){return k ==1? n :((n - k +1.0)/ k)*CalculateBinominalCoefficient(n, k -1);}}
答案6:
using System.Numerics;publicclassCheckChoose{publicstaticintCheckchoose(long m,int n){for(int x = n-1; x >= n/2; x--){if(m ==Fact(n)/(Fact(x)*Fact(n - x))){return n - x;}}return-1;}publicstaticBigIntegerFact(int n){BigInteger res =1;while(n >1){
res *= n--;}return res;}}
答案7:
using System.Collections.Generic;using System.Numerics;publicclassCheckChoose{privatestatic List<BigInteger> fs =newList<BigInteger>{BigInteger.Zero, BigInteger.One,newBigInteger(2),newBigInteger(6)};publicstaticlongCheckchoose(long m,int n){int k =0;while(k++< n){int nk = n - k;long ans =(long)(partialFact(n, nk)/factorial(k));if(m == ans)return k;if(m < ans)return-1;//No need to search the other side of the triangle}return-1;}privatestaticBigIntegerfactorial(int n){if(n ==0|| n ==1)return BigInteger.One;if(n < fs.Count)return fs[n];BigInteger f =factorial(n -1)*newBigInteger(n);
fs.Add(f);return f;}privatestaticBigIntegerpartialFact(int hi,int le){return hi <= le ? BigInteger.One :newBigInteger(hi)*partialFact(hi -1, le);}}
答案8:
publicclassCheckChoose{publicstaticlongCheckchoose(long m,int n){long c =1;for(long x =0, e = n /2; x <= e; x++){if(c >= m)return c == m ? x :-1;
c = c *(n - x)/(x +1);}return-1;}}
答案9:
using System;publicclassCheckChoose{publicstaticlongCheckchoose(long m,int n){for(var i =1; i < n; i++){var sm = Math.Round(F(n)/(F(i)*F((n - i))));if((long)sm == m)return i;}return-1;}privatestaticdoubleF(double n){return n ==1?1: n *F(n -1);}}
颜色的选择【难度:2级】:答案1:using System.Numerics;public class CheckChoose{ public static long Checkchoose(long m, long n) { BigInteger[] factorialArr = new BigInteger[n + 1]; facto...