题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3673
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. Hardyregarding 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)
Hint
Although most numbers cannot be expressible as the sum of two positive cubes, the vast majority of numbers in this task can be expressible as the sum of two positive cubes in two or more ways.
Author: ZHOU, Yuchen
Contest: ZOJ Monthly, November 2012
数学题,队友写的。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <bitset>
#include <cstdlib>
using namespace std;
#define maxsize 5000100
struct ANS
{
unsigned long long a,b;
ANS (unsigned long long x=0,unsigned long long y=0){
a=x;b=y;
}
bool operator < (const ANS &rhs) const{
return a<rhs.a;
}
};
vector<ANS> ans;
bool vis[maxsize];
int p[maxsize];
int cnt,t;
unsigned long long pf,qf;
int Euler(int n){
int i, j;
for (i = 2; i < n; ++i){
if (!vis[i]){
p[cnt++] = i;
}
for (j = 0; j < cnt && i * p[j] < n; ++j){
vis[i * p[j]] = true;
if (i % p[j]==0) break;
}
}
return cnt;
}
int stf[1010][2],stfcnt=0;
vector<int>factor;
unsigned long long N,NC;
void dfs(int deep,long long now){
if(deep==stfcnt){
factor.push_back(now);
return;
}
long long tmp=1;
for(int i=0;i<=stf[deep][1];i++){
if(tmp*now<=t)
dfs(deep+1,tmp*now);
else
return;
tmp*=stf[deep][0];
}
return;
}
int main()
{
Euler(5000000);
while(~scanf("%llu",&N)){
double doubleN=N;NC=N;
t=pow((4*doubleN),1.0/3)+5;
// t=N;
stfcnt=0;
factor.clear();
ans.clear();
for(int i=0;p[i]<=t && N>=p[i];i++){
if(N%p[i]==0){
stf[stfcnt][0]=p[i];
stf[stfcnt][1]=0;
while(N%p[i]==0){
stf[stfcnt][1]++;
N=N/p[i];
}
stfcnt++;
}
}
dfs(0,1);
for(int i=0;i<factor.size();i++){
pf=factor[i];
qf=NC/pf;
// cout<<pf<<' '<<qf<<endl;
unsigned long long tmp=4*qf-pf*pf;
if(qf>=(pf*pf)/4){
unsigned long long ti=sqrt(tmp/3+0.1);
if(tmp%3==0 && (ti*ti*3==tmp)){
unsigned long long a,b;
if((pf+ti)&1 || pf<=ti) continue;
a=(pf-ti)/2;b=(pf+ti)/2;
// cout<<pf<<' '<<ti<<' '<<a<<' '<<b<<"###"<<endl;
ANS at(a,b);
ans.push_back(at);
}
}
}
sort(ans.begin(),ans.end());
cout<<ans.size();
for(int i=0;i<ans.size();i++){
printf(" (%llu,%llu)",ans[i].a,ans[i].b);
}
puts("");
}
// 5 2 6 2 7 2 8 2
// for(int i=0;i<20;++i)
// {
// cout<<p[i]<<" ";
// }
// cout<<"hello world!"<<endl;
}