ZOJ-3673-1729【数论】【分解质因数】【DFS】【好题】
来源:互联网 发布:欧睿宇邦 知乎 编辑:程序博客网 时间:2024/05/17 07:21
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 =
设 t = a + b; ②
②代入①,因为 m =
可得,n =
–>
关键求m的约数即为(a+b)
以下是代码:
#include<bits\stdc++.h>#define ll unsigned long longusing namespace std;#define MAXN 5000001#define ANS_SIZE 505ll 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;}
- ZOJ-3673-1729【数论】【分解质因数】【DFS】【好题】
- [数论/质因数分解] 细胞分裂
- 数论 - 质因数分解
- C++ 数论-质因数分解
- HDU2421Deciphering Password(数论&质因数分解)
- 【NOIP_入门数论】分解质因数
- 【NOIP入门数论】分解质因数
- [SDOI2016] BZOJ4602 齿轮-dfs-带权并查集-数论逆元-质因数分解
- ACM数论基础之一_______质因数分解
- ACM-ZOJ 1008 DFS 一道好题
- ZOJ 1204--Additive equations【DFS && 好题】
- (Relax 数论1.10)POJ 1365 Prime Land(质因数分解)
- UVA 10892 LCM Cardinality(数论 质因数分解)
- 数论之素数(质因数分解与筛法)
- Choose and Divide,Uva10375(数论之分解质因数)
- Vijos 1786题:质因数分解
- 好题-Round #426 (Div. 2)C-二分&&思维||质因数分解-The Meaningless Game
- 51nod 1677 treecnt【Dfs+思维+数论】好题
- listview的综合应用
- 三言两语说shader(二)黑屏效果
- Leetcode 39 Combination Sum
- C#程序层面的内存分页方法
- 获取iOS设备的信息
- ZOJ-3673-1729【数论】【分解质因数】【DFS】【好题】
- 51nod1413 权势二进制
- 我的前端资源收藏
- 利用社会安全号码对学生记录构成的数组排序。1000个桶的基数排序并分三趟进行
- 辗转相除法求公约数和公倍数
- Flash as3数据类型介绍
- va_start和va_end使用详解
- 四种XML解析方式详解
- ubuntu14.04 系统加固(1)