POJ 1306 Combinations 高精度乘法

题目大意:给出mn,让你求C(m,n)。

思路:公式都给你了,就100,暴力就可以关键还是高精度。如果按照算法“它让你怎么做你就怎么做”,那么很显然你需要写一个高精度除法。然而可以证明,这个除法是不会产生余数的。所以我们可以数论分析,然后避免高精度除法。

方法就是暴力求每个数的质因数,然后把被除数和除数相同的质因数消去,最后除数肯定会被消没。这样只要做高精度乘法就可以了。

CODE:

#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define MAX 110
#define BASE 1000
using namespace std;

int n,m;
int factor[MAX];

struct BigInt{
	int num[MAX],len;

	BigInt(int _ = 0) {
		memset(num,0,sizeof(num));
		if(_) {
			len = 1;
			num[1] = _;
		}
		else	len = 0;
	}
	BigInt operator *(int a) {
		BigInt re;
		int temp = 0;
		for(int i = 1; i <= len; ++i) {
			re.num[i] = num[i] * a + temp;
			temp = re.num[i] / BASE;
			re.num[i] %= BASE;
		}
		re.len = len;
		while(temp) {
			re.num[++re.len] = temp % BASE;
			temp /= BASE;
		}
		return re;
	}
	void operator *=(int a) {
		*this = *this * a;
	}
};

ostream &operator <<(ostream &os,const BigInt &a)
{
	os << a.num[a.len];
	for(int i = a.len - 1; i; --i)
		os << fixed << setfill('0') << setw(3) << a.num[i];
	return os;
}

inline void Divide(int x,int c)
{
	for(int i = 2; i * i <= x; ++i)
		if(x % i == 0)
			while(x % i == 0) {
				factor[i] += c;
				x /= i;
			}
	if(x != 1)	factor[x] += c;
}

int main()
{
	while(scanf("%d%d",&n,&m),m + n) {
		memset(factor,0,sizeof(factor));
		for(int i = 1; i <= n; ++i)
			Divide(i,1);
		for(int i = 1; i <= m; ++i)
			Divide(i,-1);
		for(int i = 1; i <= n - m; ++i)
			Divide(i,-1);
		BigInt ans(1);
		for(int i = 1;i <= 100; ++i)
			for(int j = 1; j <= factor[i]; ++j)
				ans *= i;
		cout << n << " things taken " << m << " at a time is " << ans <<" exactly." << endl;
	}
	return 0;
}

时间: 2024-08-04 11:48:41

POJ 1306 Combinations 高精度乘法的相关文章

POJ 1306.Combinations

Combinations Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 1306 Description Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M beco

高精度乘法程序

对于超过20位的数的乘法问题,我们无法使用普通的方法!!!即使是longlong也会超出范围的!像这样的数,我们只能使用高精度的知识利用数组的方法解决问题!对于高精度乘法的问题,其实思路和高精度加法的思路差不多,都需要使用字符数组来存放每次算完的结果!        1  2  3        *4  5  6    ________________      12  15  18   8  10  124  5   6  _____________________4 13   28   27

poj1001(高精度乘法)

1.题目表述 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 135893   Accepted: 33256 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation o

真真真&#183;?高精度乘法!!!!!

RX-0奉上哈哈哈哈哈哈哈哈哈哈哈哈哈™™™ 先奉上真真真·高精度乘法源代码: 高精度乘法 RX-0制作最后修改:2016年7月6日#include<stdio.h>#include<string.h>#include<math.h>char s[10000],b;int a[10000];int c[10000];int main(){ int x,l=0,y=1,i,j,m,l1=0; long long s1=0; //freopen("hp.in&qu

[转]高精度乘法计算

转载自: Daywei 高精度乘法计算 高精度乘法计算基础 1.高精度浮点运算方法 高精度浮点(Floating Point,FP)运算可以转换成整数型运算.由于高精度浮点数可以看成是由整数部分(Integer Part,IP)与小数部分(Decimal Part,DP)的组合,因此其乘法可以看成以下3种运算的组合,即整数x整数(IxI).整数x小数(IxD)和小数x小数(DxD).用表达式表示, 则FP1*FP2=IP1*IP2+(IP1*DP2+IP2*DP1)+DP1*DP2 (1)对于I

[vijos P1040] 高精度乘法

如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了,好在我机智,一看到解题里说要压位就自动脑补出压位了. 代码风格非常诡异,弱智题竟然写到2KB我也是醉了. program vijos_p1040; const maxn=10020; var a,b,aa,bb:array[1..maxn] of integer; c:array[1..2*maxn

【PKU1001】Exponentiation(高精度乘法)

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

HDU 1042.N!【高精度乘法】【8月24】

N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 高精度乘法.数组存,每一位存5位数,不然会严重超时.另外,

高进度加法与高精度乘法

正整数的高精度加法和高精度乘法(C++) 加法运算如下: // 高精度加法 string add(string a, string b) { // 确保 a >= b if (a.size() < b.size()) { string temp = a; a = b; b = temp; } int len1 = a.size(), len2 = b.size(); // a, b前缀补零 ,比如 a = 12345, b = 678时,补零之后 a = 012345,b = 0000678.