UVALive - 8265 Coefficient Computation(大整数)

题目链接

题目大意:

求组合数 \frac{n!}{r!(n-r)!} 并按要求的进制输出。

看桑老师用java写的,却因为不会开数组最后废掉了,哈哈哈哈。

不说了看我桑嘤嘤的java代码:

import java.math.*;
import java.util.*;
 
public class Main
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int n,k,d,t;
		t = in.nextInt();
		for(int o = 0; o < t; o++)
		{
			n = in.nextInt(); 
			k = in.nextInt();
			d = in.nextInt();
			if(n == 0 || k == 0)
				System.out.println(1);
			else
			{
				BigInteger big = BigInteger.valueOf(n);
				for(int i = n-1; i >= n-k+1; i--)
				{
					big = big.multiply(BigInteger.valueOf(i));
				}
				BigInteger lit = BigInteger.valueOf(k);
				for(int i = k-1; i > 0; i--)
				{
					lit = lit.multiply(BigInteger.valueOf(i));
				}
				BigInteger ans = big.divide(lit);
				BigInteger[] temp = new BigInteger[50010];//!!!!!!!!!!!!!!!!!!!!!!!!!开数组
				BigInteger dd = BigInteger.valueOf(d);
				int index = 0;
				while(true)
				{
					if(ans.compareTo(BigInteger.valueOf(0)) == 0) break;
					temp[index++] = ans.mod(dd);
					ans = ans.divide(dd);
				}
				for(int i = index-1; i>=0; i--)
					System.out.print(temp[i]);
				System.out.println();
			}
		}
	}
}

 

  但 其实还有更简单的java代码

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
	public static BigInteger C(int n,int k)
	{
	BigInteger ans = BigInteger.valueOf(1);
	for(int i = 2; i <= n; i++)
	{  
		ans = ans.multiply(BigInteger.valueOf(i));
	}
	for(int i = 2; i <= n-k; i++)
	{
		ans = ans.divide(BigInteger.valueOf(i));
	}
	for(int i = 2; i <= k; i++)
	{
		ans = ans.divide(BigInteger.valueOf(i));
	}
	return ans;
	}
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		for(int t = 0; t < T; t++)
		{
			int n = in.nextInt();
			int k = in.nextInt();
			int d = in.nextInt();
			BigInteger ans = C(n,k);
			System.out.println(ans.toString(d));//这个函数直接按d进制输出
		}
	}

}

当然也有C++的代码,不过都是板子了

#include <bits/stdc++.h>
using namespace std;
#define maxn  9999

class BigNum
{
public:
    int a[500];
    int len;
public:
    BigNum()
    {
        len=1;
        memset(a,0,sizeof a);
    }
    BigNum(const int );
    BigNum(const char*);
    BigNum(const BigNum&);
    BigNum &operator = (const BigNum &);
    friend istream &operator>>(istream &,BigNum &);
    friend ostream &operator<<(ostream &,BigNum &);
    BigNum operator*(const BigNum &)const;
    BigNum operator/(const int &)const;
    int operator%(const int &)const;
    bool operator ==(const int ans)const
    {
        if(len==1&&a[0]==0)
            return true;
        return false;
    }
    void print();
};
BigNum::BigNum(const int b)
{
    int c,d=b;
    len=0;
    memset(a,0,sizeof a);
    while(d>maxn)
    {
        c=d-(d/(maxn+1))*(maxn+1);
        d=d/(maxn+1);
        a[len++]=c;
    }
    a[len++]=d;
}
BigNum & BigNum::operator=(const BigNum &n)
{
    int i;
    len= n.len;
    memset(a,0,sizeof a);
    for( i=0; i<len; i++)
        a[i]=n.a[i];
    return *this;
}
BigNum BigNum::operator*(const BigNum &T)const
{
    BigNum ret;
    int i,j,up;
    int temp,temp1;
    for(i=0; i<len; i++)
    {
        up=0;
        for(j=0; j<T.len; j++)
        {
            temp=a[i]*T.a[j]+ret.a[i+j]+up;
            if(temp>maxn)
            {
                temp1=temp-temp/(maxn+1)*(maxn+1);
                up=temp/(maxn+1);
                ret.a[i+j]=temp1;
            }
            else
            {
                up=0;
                ret.a[i+j]=temp;
            }
        }
        if(up!=0)
            ret.a[i+j]=up;
    }
    ret.len=i+j;
    while(ret.a[ret.len-1]==0&&ret.len>1)
        ret.len--;
    return ret;
}

BigNum BigNum::operator/(const int &b)const
{
    BigNum ret;
    int i,down=0;
    for(i=len-1; i>=0; i--)
    {
        ret.a[i]=(a[i]+down*(maxn+1))/b;
        down=a[i]+down*(maxn+1)-ret.a[i]*b;
    }
    ret.len=len;
    while(ret.a[ret.len-1]==0&&ret.len>1)
        ret.len--;
    return ret;
}
int BigNum::operator %(const int &b)const
{
    int i,d=0;
    for(i=len-1; i>=0; i--)
    {
        d=((d*(maxn+1))%b+a[i])%b;
    }
    return d;
}
void  BigNum::print()
{
    int i;
    printf("%d",a[len-1]);
    for(int i=len-2; i>=0; i--)
        printf("%04d",a[i]);
    printf("\n");
}
int res[1000];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m,k;
        cin>>n>>m>>k;
        BigNum ans(1);
        for(int i=1; i<=m; i++)
            ans = ans*BigNum(n-i+1)/i;
        int cnt=0;
        while(!(ans==0))
        {
            res[cnt++]=ans%k;
            ans=ans/k;
        }
        for(int i=cnt-1; i>=0; i--)
            cout<<res[i];
        cout<<endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值