【POJ - 2917】Diophantus of Alexandria(推公式)

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 xn + yn = zn. Fermat suggested that for n > 2, there are no solutions with positive integral values for xy 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)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:

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 nquickly?

Input

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

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

题意:

给了一个公式,给你n,你需要找出有多少对(x,y)符合这个式子。

思路:

因为n的范围很大,所以第一想法,这个题目需要推公式。

原式等价于\frac{1}{y}=\frac{x-n}{xn}

所以我们可以得出y=\frac{xn}{x-n}.

观察给出样例,我们可以得出结论\frac{1}{x}\frac{1}{y}这两个数是关于\frac{1}{2n}分布。当我们假定x<y我们可以得出x的取值范围[n+1,2n]。所以我们假设存在一个i(i的范围为1~n)则x=n+i。代入上述式子。

可得y=\frac{n^2}{i}+n。我们已经保证x有意义,所以我们能推出一个y只要我们能保证y也有意义,就可得出结果。

y有意义就是n^2是i的倍数。所以我们的问题就转化成了n^2的因子个数有多少个,其中要求这些因子是属于1~n的。

对于这个问题,我们可以发现,n^2的因子是关于n对称分布的,就是有一个因子小于n,那么必定有一个和他对应的因子大于n。

所以我们只需要求出n^2的因子个数,再加1,然后除以2就可以。加1是为了保证把n算上。那么我们只要求n^2的因子个数,这里我们可以借助欧拉函数,分解一下质因子,并求出其个数。所以这里我们对n进行处理,因为n^2的某个质因子的个数正好是n的两倍,这样我们可以优化一下,保证不会超时。

具体细节可以看代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;

void phi(ll n)
{
	ll t=1;
	for(ll i=2;i*i<=n;i++)
	{
		ll q=0;
		if(n%i==0)
		{
			while(n%i==0)
			{
				q++;
				n/=i;
			}
		}
		t=(t*(2*q+1));
	}
		
	if(n>1) {
		t*=3;//某个质因子只有1个,然后n^2的就有2个,再加一
	}
	printf("%lld\n",(t+1)/2);
}
int main()
{
	ll t,n;
	scanf("%lld",&t);
	int tt=1;
	while(t--) 
	{
		scanf("%lld",&n);
		printf("Scenario #%d:\n",tt++);
		phi(n);
		if(t)
		printf("\n");
	}
	return 0; 
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值