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; }
相关文章推荐
- 利用社会安全号码对学生记录构成的数组排序。1000个桶的基数排序并分三趟进行
- 特征选择之卡方统计 Chi-Square
- unity3D:Unity中的优化技术
- C#程序层面的内存分页方法
- Leetcode 39 Combination Sum
- 获取iOS设备的信息
- tortoisesvn › prefer local prefer repository
- Java虚拟机学习之内存区域与内存溢出
- listview的综合应用
- struts2-知识点:开发模式,源码查看
- 编译加速
- AngularJs 自定义filter
- Ural 1209. 1, 10, 100, 1000... 一道有趣的题
- Struts2运行原理
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter7-30
- 无线监听工装使用说明
- LaTeX数学公式
- IAR之目标文件内存分配
- 一、初学SpringMVC+Mybatis之Spring简介
- 动态分配的二维字符数组 赋值时记得加‘\0’