hdu 1299 Diophantus of Alexandria(数论(因子个数和))

本文详细解析了迪奥方程的求解思路,通过数学推导和编程实现,提供了求解特定形式迪奥方程的算法,并附带了C++代码示例。对于数学爱好者和编程初学者来说,是一篇实用的技术指南。

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1299

Problem Description
Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first mathematicians to study equations where variables were restricted to integral values. In honor of him, these equations are commonly called diophantine equations. One of the most famous diophantine equation is x^n + y^n = z^n. Fermat suggested that for n > 2, there are no solutions with positive integral values for x, y and z. A proof of this theorem (called Fermat's last theorem) was found only recently by Andrew Wiles.

Consider the following diophantine equation:

1 / x + 1 / y = 1 / n where x, y, n ∈ N+ (1)


Diophantus is interested in the following question: for a given n, how many distinct solutions (i. e., solutions satisfying x ≤ y) does equation (1) have? For example, for n = 4, there are exactly three distinct solutions:

1 / 5 + 1 / 20 = 1 / 4
1 / 6 + 1 / 12 = 1 / 4
1 / 8 + 1 / 8 = 1 / 4



Clearly, enumerating these solutions can become tedious for bigger values of n. Can you help Diophantus compute the number of distinct solutions for big values of n quickly?
 

Input
The first line contains the number of scenarios. Each scenario consists of one line containing a single number n (1 ≤ n ≤ 10^9).
 

Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Next, print a single line with the number of distinct solutions of equation (1) for the given value of n. Terminate each scenario with a blank line.
 

Sample Input
  
2 4 1260
 

Sample Output
  
Scenario #1: 3 Scenario #2: 113
1 / x + 1 / y = 1 / n where x, y, n ∈ N+ (1)
分析:令y=n+k,1/x+1/(n+k)=1/n --> (n+k)n=x*n+x*(n+k)=2*n*x+x*k -->x=(n+k)n/(2n+k) NO!NO!!  x=n(n+k)/k=n*n/k+n 目标要明确!接下来就是对n*n找约数个数了。把那条展开式印在脑子里,约数个数等于各个(素因子个数+1)的乘积。由于1 ≤ n ≤ 10^9,所以不要n*n再分解。计算约数的个数,素因子相同是加,不同是乘。比如上一篇博客中:2004=2^2*3*167,2004所有因子的情况:(1+2+2^2)*(1+3)*(1+167)。所以2004的因子个数和就是3*2*2=12。同样这个规律适用于n*n,它相比n而言相当于素因子个数增加了一倍,单个素因子的个数是:素因子个数*2,最后乘的时候各个(素因子个数+1)相乘。因为要求 x ≤ y,所以结果要取ans/2的整数上界,即(ans+1)/2.
另外在做题的时候,发现用线性筛法再分解有超时现象, 还不如把所给数字直接素因子分解
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5;
int sta[maxn],pnum[maxn],top;
void fenjie(int m){
    top=0;
    memset(sta,0,sizeof(sta));
    memset(pnum,0,sizeof(pnum));
    for(int i=2;i*i<=m;i++){
        if(m%i==0){
            sta[top]=i;
            while(m%i==0){
                m/=i;
                pnum[top]++;
            }
            top++;
        }
    }
    if(m>1){
        sta[top]=m;
        pnum[top++]++;
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,ca;
    scanf("%d",&t);
    for(ca=1;ca<=t;ca++){
        int n;
        scanf("%d",&n);
        fenjie(n);
        int ans=1;
        for(int i=0;i<top;i++){
            ans*=(2*pnum[i]+1);
        }
        printf("Scenario #%d:\n%d\n\n",ca,(ans+1)/2);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值