码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 12005 Find Solutions (最小因子分解)

时间:2014-08-16 22:33:01      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

Description

bubuko.com,布布扣


  Find Solutions 

Look at the following equation:

c = ab - bubuko.com,布布扣 + 1

Now given the value of c, how many possible values of and a and b are there (a and b must be positive integers)? That is you will have to find the number of pairs (a, b) which satisfies the above equation.

Input 

The input file contains around 3000 line of input. Each line contains an integers n ( 0 < nbubuko.com,布布扣1014). This n actually denotes the value of c. A line containing a single zero terminates the input. This line should not be processed.

Output 

For each line of input produce one line of output. This line contains two integers. First integer denotes the value of c and the second integer denotes the number of pair of values of a and b that satisfies the above equation, given the value of c.

Sample Input 

1020
400
0

Sample Output 

1020 8
400 2
题意:求等式是c的所有可能

思路:将c=a?b?a+b2+1 因式分解后得到4?c?3=(2?a?1)?(2?b?1)

所以这道题目就可以转换为求4*c-3的因数的组成了,在求出所有的因子的质数后,就是用隔板法将f[i]拆成2个,就是乘以f[i]+1.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 22000000;

int f[maxn], b[maxn];
int lp, p[maxn>>3], pri[maxn];

void init() { // pri[] 最小的因子
	lp = 0;
	for (int i = 2; i < maxn; i++) {
		if (!pri[i])
			p[lp++] = pri[i] = i;
		for (int j = 0; j < lp && i * p[j] < maxn; j++) {
			pri[i * p[j]] = p[j];
			if (i % p[j] == 0)
				break;
		}
	}
}

void cal(ll n, ll &l, int b[], int f[]) {
	ll tmp, i = 0;
	l = 0;
	while (n > 1) {
		if (n < maxn)
			tmp = pri[n];
		else {
			tmp = n;
			for (; i < lp && n/p[i] >= p[i]; i++)
				if (n % p[i] == 0) {
					tmp = p[i];
					break;
				}
		}
		f[l] = 0;
		while (n % tmp == 0) {
			n /= tmp;
			f[l]++;
		}
		b[l++] = tmp;
	}
}

int main() {
	ll n, l;
	init();
	while (scanf("%lld", &n) != EOF && n) {
		cal(4*n-3, l, b, f);
		ll sum = 1;
		for (int i = 0; i < l; i++)
			sum *= f[i] + 1;
		printf("%lld %lld\n", n, sum);
	}
	return 0;
}


UVA - 12005 Find Solutions (最小因子分解),布布扣,bubuko.com

UVA - 12005 Find Solutions (最小因子分解)

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://blog.csdn.net/u011345136/article/details/38618915

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!