一分钟内算出21位数的水仙花数
来源:互联网 发布:淘宝客服工资制度 编辑:程序博客网 时间:2024/06/11 18:45
#include<iostream>#include <cstring>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdlib>#include<algorithm>#define LL long long#define M ((LL)100000000000000)#define n (21)using namespace std;bool cmp(LL a,LL b){ return a>b;}LL len(LL x){ LL i=0; while(x) i++,x/=10; return i;}struct bigint{ LL a2,a1; bigint(LL b2=0,LL b1=0){a2=b2;a1=b1;if(::len(b1)>14 || ::len(b2)>14) cout<<"err3"<<endl;} bigint operator+(bigint a) { LL c=(a.a1+a1)/M; LL aa1=(a.a1+a1)%M; LL aa2=a.a2+a2+c; return bigint(aa2,aa1); } bigint operator+(LL a) { return bigint(a2,a1)+bigint(a/M,a%M); } bigint operator-(bigint a) { LL c=0; if(a1<a.a1) c=1; if(a2<a.a2 || (a2==a.a2 && c)) cout<<"err1"<<endl; LL aa1=(M+a1-a.a1)%M; LL aa2=a2-a.a2-c; return bigint(aa2,aa1); } bigint operator*(LL a) { LL c=(a1*a)/M; LL aa1=(a1*a)%M; LL aa2=a2*a+c; return bigint(aa2,aa1); } bool operator<(bigint a) { if(a2<a.a2) return true; if(a2==a.a2 && a1<a.a1) return true; return false; } LL len() { LL ans=0; if(a2>0) return ::len(a2)+14; return ::len(a1); } void display() { if(a2==0) { cout<<a1<<endl; return; } cout<<a2; cout.fill('0'); cout.width(14); cout<<a1<<endl; }};bigint mpow[10],mj[22];LL a[30];void mypow(){ mpow[0]=bigint(); mpow[1]=bigint(0,1); LL i=2; for(;i<=9;i++) { mpow[i]=mpow[1]; LL m=n; while(m--) mpow[i]=mpow[i]*i; }}bool judge(bigint b){ if(b.len()!=n) return false; LL c[30],i=0,a1=b.a1,a2=b.a2; while(a1) { c[i++]=a1%10; a1/=10; } i=14; while(a2) { c[i++]=a2%10; a2/=10; } sort(c,c+n,cmp); for(i=0;i<n;i++) if(a[i]!=c[i]) {return false;} return true;}bool abcd=0;void dfs(int x,int p,bigint t){ if(abcd) return; if(t.len()>n) return; if(x>=n) { if(t.len()<n) abcd=1; if(judge(t)) t.display(); return; } for(;p>=0;p--) { if(abcd) return; a[x]=p; dfs(x+1,p,t+mpow[p]); }}int main(){ mypow(); dfs(0,9,bigint(0,0)); return 0;}
0 0
- 一分钟内算出21位数的水仙花数
- 求21位数的水仙花数
- 求21位数的水仙花数
- 求21位数的水仙花数
- 【蓝桥杯】【求21位数的水仙花数】
- 水仙花数:n位数的水仙花数
- 3位数的水仙花数
- 算法练习-求21位数的水仙花数
- 打印任意位数的水仙花数
- java__for水仙花数,求数的位数上的数
- 水仙花数(3位数)
- 2. 打印出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如:153是一水仙花数,因为153=1^3+5^3+3^3.
- 求任意位数的水仙花数(阿姆斯特朗数)
- JavaScript中打印3位数中所有的水仙花数
- java所有N位数之内的自幂数(3位数的自幂数也叫水仙花数)
- C#打印水仙花数(3位数)
- 20171206 水仙花数<一>
- 求N位数的所有自然回归数(水仙花数)
- javascript高级程序设计学习笔记三
- iOS编程技巧总结之语音篇--前言
- android gridview保留点击后的背景黄色
- Js获取当前日期时间及其它操作
- gdb debug tutorial
- 一分钟内算出21位数的水仙花数
- 第十一章作业
- JAXB API
- 【互动出版网】2013双12全场科技类图书6.5折封顶
- 黑马程序员 多线程(二)。
- 液晶屏 -- 驱动板 -- 屏线 -- 高压条 -- LVDS接口知识
- I2C通信协议
- 深入理解JavaScript系列
- CSS 颜色代码