您的位置:首页 > 其它

ZOJ-3673-1729【数论】【分解质因数】【DFS】【好题】

2016-03-25 20:16 716 查看

3673-1729

Time Limit: 3 Seconds      Memory Limit: 65536 KB


1729 is the natural number following 1728 and preceding 1730. It is also known as the Hardy-Ramanujan number after a famous anecdote of the British mathematician G. H. Hardy regarding a hospital visit to the Indian mathematician Srinivasa Ramanujan. In Hardy’s words:

I remember once going to see him when he was ill at Putney. I had ridden in taxi cab number 1729 and remarked that the number seemed to me rather a dull one, and that I hoped it was not an unfavorable omen. “No,” he replied, “it is a very interesting number; it is the smallest number expressible as the sum of two (positive) cubes in two different ways.”

The two different ways are these: 1729 = 13 + 123 = 93 + 103

Now your task is to count how many ways a positive number can be expressible as the sum of two positive cubes in. All the numbers in this task can be expressible as the sum of two positive cubes in at least one way.

Input

There’re nearly 20,000 cases. Each case is a positive integer in a single line. And all these numbers are greater than 1 and less than 264.

Output

Please refer to the sample output. For each case, you should output a line. First the number of ways n. Then followed by n pairs of integer, (ai,bi), indicating a way the given number can be expressible as the sum of ai’s cube and bi’s. (ai≤ bi, and a1< a2< …< an)

Sample Input

9

4104

2622104000

21131226514944

48988659276962496

Sample Output

1 (1,2)

2 (2,16) (9,15)

3 (600,1340) (678,1322) (1020,1160)

4 (1539,27645) (8664,27360) (11772,26916) (17176,25232)

5 (38787,365757) (107839,362753) (205292,342952) (221424,336588) (231518,331954)

题目连接:ZOJ-3673

题目大意:给出一个数字m,求满足m = a^3 + b^3(a,b为整数)的所有整数对。

题目思路:

已知 m =a3+b3=(a+b)(a2−a∗b+b2) a^3 + b^3 = (a + b) (a^2 - a*b + b^2) ①

设 t = a + b; ②

②代入①,因为 m = (a+b)((a+b)2−3∗a∗b)(a + b)((a + b) ^ 2 - 3 *a * b)

可得,n = a∗b=(t2−m/t)/3 a * b = (t^2 - m / t) / 3

–>a2+a∗t+n=0a^2 + a*t + n = 0; 求出a可得b

关键求m的约数即为(a+b)

以下是代码:

#include<bits\stdc++.h>
#define ll unsigned long long
using namespace std;
#define MAXN 5000001
#define ANS_SIZE 505
ll f[ANS_SIZE],nf[ANS_SIZE];  //f存放质因数,nf存放对应质因数的个数
ll plist[MAXN], pcount=0;
bool isPrime[MAXN+1];
void initprime()//素数且不说,所有合数都能分解成任意素数之积
{
int i,j;
pcount = 0;
for(i = 2; i<MAXN; i++)
{
if(!isPrime[i]) plist[pcount++] = i;//打下素数表
for(int j = 0; j<pcount && i*plist[j]<MAXN; j++)
{
isPrime[i*plist[j]] = true;//所有非素数排除
if(i%plist[j]==0) break;
}
}
}

int prime_factor(ll n) {
int cnt = 0;
int n2 = sqrt((double)n);
for(int i = 0; n > 1 && plist[i] <= n2 && i < pcount; ++i)
{
if (n % plist[i] == 0) {
for (nf[cnt] = 0; n % plist[i] == 0; ++nf[cnt], n /= plist[i]);
f[cnt++] = plist[i];
}
}
if (n > 1) nf[cnt] = 1, f[cnt++] = n;
return cnt;   //返回不同质因数的个数
}

vector<ll> yue;
void dfs( int x, int t, ll ss ){
if( x==t ) return;
dfs( x+1, t, ss);
for( int i=0 ; i<nf[x] ; i++ ){
ss *= f[x];
yue.push_back(ss);
dfs( x+1, t, ss);
}
}
vector <pair<ll,ll> > ans;
bool cmp(pair<ll,ll> a,pair<ll,ll> b)
{
if (a.first != b.first)
{
return a.first < b.first;
}
return a.second < b.second;
}
int main(){
ll m;
initprime();
while(cin >> m)
{
ans.clear();
yue.clear();
int ret = prime_factor(m);
yue.push_back (1);
dfs(0,ret,1);
memset(f,0,sizeof(f));
memset(nf,0,sizeof(nf));
for (int i = 0;  i < yue.size(); i++)
{
long long t = yue[i];
if (m % t != 0) continue;
if ((t * t - m / t) % 3 != 0) continue;
long long q = (t * t - m / t) / 3;
if (q < 0) continue;
long long dea = t * t - 4 * q;
if ((ll)sqrt(dea * 1.0) * (ll)sqrt(dea * 1.0) != dea) continue;
long long a = t + (ll)sqrt(dea * 1.0);
if (a % 2 != 0) continue;
a = a / 2;
long long b = t - a;
if (b > 2642246 || a > 2642246) continue;
ans.push_back(make_pair(min(a,b),max(a,b)));

}
sort(ans.begin(),ans.end(),cmp);
cout << ans.size();
for (int i = 0; i < ans.size(); i++)
{
printf(" (%lld,%lld)",ans[i].first,ans[i].second);
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: