您的位置:首页 > 运维架构

Hoj 2305 Diophantus of Alexandria

2013-04-15 22:00 309 查看
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2305

题意:给定的数n,求1/x + 1/y = 1/n 有多少种情况,x<=y,都是正整数。

我们很容易求得:n<x<=2*n.

第一种思路是枚举遍历x,判断y是不是正整数就行了,注意判断的手段,用floor(y+0.5)?= y来判断。但是很不幸会超时。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int solve(int n)
{
int ans = 0;
for(int x=n+1;x<=2*n;x++)
{
double y = ((double)n*x)/(x-n);
if(floor(y + 0.5) == y) ans++;
}
return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
scanf(" %d",&t);
int cas = 0;
int n;
while(t--)
{
cas++;
scanf(" %d",&n);
printf("Scenario #%d:\n",cas);
printf("%d\n",solve(n));
printf("\n");
}
return 0;
}


第二种方法就是因子分解,我们可以整理成(x-n)*(y-n) = n^2

那么,我们求n^2的因子个数就行了。我们只要会求n的因子个数就行了。我们可以采取任何一个数都可以分解成多个素数的幂的乘机这种方式。枚举时可以枚举sqrt(n)以内的素因子,因为剩下的大于sqrt(n)的因子肯定是一个素数。它自己的因子数是2.平方以后就是3了。

见代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int solve(int n)
{
int ans = 1;
for(int i=2;i<=sqrt(n);i++)
{
if(n%i == 0)
{
int t = 0;
while(n%i == 0)
{
t++;
n = n/i;
}
ans *= (t*2+1);
}
}
if(n!=1) ans*=3;
ans = (ans + 1)/2;
return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
scanf(" %d",&t);
int cas = 0;
int n;
while(t--)
{
cas++;
scanf(" %d",&n);
printf("Scenario #%d:\n",cas);
printf("%d\n",solve(n));
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: