hdu 1133 Buy the Ticket (高精乘法,不需高精除法)

原题链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=3&problemid=6


在看过网上的很多解答后,感觉大同小异。


我的做法:

1.不需要开二维数组。

2.不需要高精除法 (n!=0时,必定有m+1<=m+n

3.不需要memcpy函数


分析:

1.n>m 很明显0种情况

2.直接求有多少正确的不同的队列数是比较难的。

所以利用 全部的情况-错误的情况 求得。

C(m+n,n)全部的情况,从m+n个位置选取n个位置为100元的。

C(m+n,m+1)错误的情况,从m+n个位置选取m+1个位置为100元的。

所以公式为(C(m+n,n)-C(m+n,m+1))*(m!)*(n!)

化简得:(m+n)!*(  (m-n+1)  /  (m+1)  )


代码如下:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int base = 10000;
int BigInt[100];
void BigMul(int b)
{
	int  temp = 0;
	for (int i = 99; i >= 0; i--)
	{
		temp += BigInt[i] * b ;
		BigInt[i] = temp%base;
		temp /= base;
	}
	
}

void JC(int n, int fm)
{
	for (int i = n; i >= 2; i--)
	{
		if (i!=fm)			//分母为m+1  n!=0时,必定有 m+1<=m+n   所以在相乘时直接约去。
		BigMul(i);
	}
}

int main()
{
	int n, m, k = 0;
	while (cin >> m >> n && (n || m))
	{
		memset(BigInt, 0, sizeof(BigInt));
		BigInt[99] = 1;					//别忘了!!!
		cout << "Test #" << ++k << ":\n";
		if (n > m)
		{
			cout << 0 << endl;
			continue;
		}
		if (n != 0)
		{
			BigMul(m - n + 1);
			JC(m + n, m + 1);
		}
		else		//n为0的情况
		{
			JC(m, 0);
		}
		int j = 0;
		while (!BigInt[j])j++;		//去掉前面的0
		printf("%d", BigInt[j++]);		//最高位不需要补齐4位
		while (j<=99)
		{
			printf("%04d", BigInt[j]);
			j++;
		}
		cout << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值