设为首页 加入收藏

TOP

UVA - 12005 Find Solutions (最小因子分解)
2015-07-20 17:53:38 来源: 作者: 【 】 浏览:2
Tags:UVA 12005 Find Solutions 最小 因子 分解

Description

Download as PDF


Find Solutions

Look at the following equation:

c = ab - $\displaystyle {\frac{{a+b}}{{2}}}$ + 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 < n$ \le$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 
   
    
#include 
    
      #include 
     
       #include 
      
        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; }
      
     
    
   


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++新特性 右值引用 移动构造函数 下一篇C++代码注释行和函数个数统计

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: