hdu 1299 Diophantus of Alexandria

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

题目大意:就是给你一个n,求1/x+//y=1/n正整数解的个数。

思路:首先我们可以在两边同时乘以xyn得yn+xn=xy,然后因式分解的(x-n)*(y-n)=n*n。这题要求解的个数,就变成了求n*n的因子数,网上很多人都是令y=n+k,然后的x=(n*n)/k+n,这儿也是求n*n的因子数。

通过数论的中的学习我们知道任意一个数n都可以由素数表示出来,即:

n=p1^e1*p2^e2*p3*e3*......pn^en 。(p1,p2,p3......pn都为素数)

因子数为:(1+e1)*(1+e2)*(1+e3)*......*(1+en)      所以对  n*n

n*n=p1^2e1*p2^2e2*p3^2e3......pn^2en

因子数为:(1+2*e1)*(1*2*e2)*(1+2*e3)*......*(1+2*en)

所以我们只需要求求出n的素因子。

code:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;

int vis[40005];
int prime[40005];

int  primetable()
{
    //int m=sqrt(40000+0.5);
    int c=0;
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=40000;i++)
    {
        if(!vis[i])
        {
            prime[c++]=i;
            for(int j=i*i;j<=40000;j+=i)
            {
                vis[j]=1;
            }
        }
    }
    return c;
}

int main()
{

    int i,kk=0;
    int len,T,n,cnt,sum1;
    len=primetable();
    scanf("%d",&T);
    while(T--)
    {
        kk++;
        cnt=1;
        scanf("%d",&n);
        for(i=0;i<len;i++)  //计算素因子的个数
        {
            int m=sqrt(n)+1;
            if(prime[i]>m) break;
            int sum1=0;
            while(n%prime[i]==0)
            {
                sum1++;
                n=n/prime[i];
            }
            cnt*=(1+2*sum1);
        }
        if(n>1) cnt*=3;     //为什么这儿要乘以3,因为前面我们一直在做n/prime[i],但是做到最后不一定n==1,但是这时这个数却不能再被分解啦,所以这个数一定是一个素数,所以要做cnt*(1+2*1)
        printf("Scenario #%d:\n%d\n\n",kk,(cnt+1)/2);  //x与y交换只能算一种,所以要除以2,但是n也是因子之一啊,我们必须加上它做成两个它,再除以二就是了
    }
    return 0;
}
时间: 2024-08-03 15:48:14

hdu 1299 Diophantus of Alexandria的相关文章

hdu 1299 Diophantus of Alexandria(数学题)

题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可以发现这个答案只与这个数的因子有关. n=a1^p1*a2^p2*...*an^pn ans=((1+2*p1)*(1+2*p2)*...*(1+2*pn)+1)/2 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;

hdu 1299 Diophantus of Alexandria (数论)

Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2269    Accepted Submission(s): 851 Problem Description Diophantus of Alexandria was an egypt mathematician living in Ale

hdu 1299

这个方法太厉害了 别人的记录一下 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> #include<iomanip> using namespace std; #define MAX_PRIME 31700 #define PRIME_NUM 3500 int

hdu 1299 数论 分解素因子

题意:求1.1/x+1/y=1/z 给定z 求x,y的个数zsd:1: 要知道这个素数的因子的范围 范围为2--sqrt(n);2: 带入方程得:x = n * n / k + n ; 现在就变成了求x的值.又因为n一定大于k,所以转换成求n * n的分解数: 因为n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en sum ( n)= ( 1 + e1 ) * ( 1 +e2 ) * .........* ( 1 +en ); sum (n * n) = ( 1

hdu-1299 Diophantus of Alexandria(分解素因子)

思路: 因为x,y必须要大与n,那么将y设为(n+k);那么根据等式可求的x=(n2)/k+n;因为y为整数所以k要整除n*n; 那么符合上面等式的x,y的个数就变为求能被n*n整除的数k的个数,且k>=n; 那么k就在n到n*n之间,不过这样不则么好求. 因为x,y具有对称性,从y=(n+k)和x=(n2)/k+n;也可以看出(n*n/k,和k等价因为k*(n*n/k)=n*n): 由于任何数都可以拆成素数的乘积,形式为a1^x1*a2^x2*a3*^x3......; 那么因数的个数为(1+

poj 2917 Diophantus of Alexandria 因数分解解1/x+1/y=1/n

题意: 给定n,求丢番图方程1/x+1/y=1/n(x<=y)的解数. 分析: 设x=n+a,y=n+b,化简可得n^2=a*b.设n^2的因子个数为p,n^2的所有因子中除n外都是成队出现的,故方程解数为(p+1)/2. 代码: //poj 2917 //sep9 #include <iostream> using namespace std; int num[64]; int main() { int cases,t=0; scanf("%d",&case

hdoj-1299-Diophantus of Alexandria【判断素因子个数+组合数】

Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2853 Accepted Submission(s): 1099 Problem Description Diophantus of Alexandria was an egypt mathematician living in Alexand

HDU1299&amp;&amp;SPOJ - KPEQU(找因子来找解的个数)

题目链接:HDU 1299 传送门 题意: 求方程1/x+1/y=1/n的解的个数 1/3+1/2 与1/2+1/3看作是一组解. 分析: 1/x+1/y = 1/n 设y = n + k; ==>1/x + 1/(n+k)=1/n; ==>x = n^2/k + n; 因为x为整数,k就是n^2的约数. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <a

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************