水仙花数
链接:https://ac.nowcoder.com/acm/contest/2763/D
来源:牛客网
题目描述
说到水仙花数,那当然是道谭浩翔书里的经典水题啦~
原题是这样的:水仙花数是指一个三位数,各位数字的立方和等于它本身。求所有的水仙花数。
不过,作为ACM选拔赛,自然是不会出这种既原又水的题的。谭浩翔特意又为大家准备了X·水仙花数。一个n位正整数,只要它的各位数字的n次方和的X倍等于该数本身,它就是一个X·水仙花数~
当然,谭浩翔还是很水的。他并不准备让你求所有的X·水仙花数。现在,他给了你一个范围[n,m],让你求n~m中所有的X·水仙花数。
输入描述:
有多组输入。
每组输入两行。
第一行输入一个正整数X(1≤X≤109)X(1\le X\le 10^9)X(1≤X≤109)
第二行输入两个整数n,m(1≤n≤m≤109)n,m(1\le n\le m\le 10^9)n,m(1≤n≤m≤109),代表一个[n,m]的闭区间
具体含义见题目描述
输出描述:
对于每组输入,从小到大输出[n,m]内所有的X·水仙花数,一行一个。数据保证至少有一个。每组输出后额外输出一个空行。
示例1
输出
复制153 370 371 407 20
题解:快速幂 + 优化计算的位数还是TLE
#include <bits/stdc++.h> using namespace std; long long solve[9]; long long quickpower(long long a,long long b) { long long s=1; while(b>0){ if(b%2==1){ s=s*a; } a=a*a; b=b>>1; } return s; } long long get_len(int n){ int count=0; while(n!=0){ n/=10; count++; } return count; } int main(){ long long x,n,m,c; while(scanf("%lld",&x)!=EOF){ scanf("%lld %lld",&n,&m); long long len = get_len(n); if(len==1) c=1; else c=pow(10,len-1); for(int i=0;i<10;i++) solve[i]=quickpower(i,len); for(long long i=n;i<=m;i++){ long long k=i; long long r=0; while(k!=0){ int d=k%10; if(i/c>=10){ for(int j=0;j<10;j++) solve[j]=solve[j]*j; c*=10; } long long t=solve[d]; //long long t=quickpower(d,len); r+=t; k/=10; } if(i==r*x) printf("%lld\n",i); } printf("\n"); } return 0; }
方法一:预算暴力打表
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> #define fir first #define se second #define ll long long #define pb push_back #define mp make_pair #define ull unsigned long long #define cl(a, b) memset(a, b, sizeof(a)) #define quickio(a) ios::sync_with_stdio(a) #define datatest() freopen("data.in", "r", stdin) #define pii pair<int, int> #define pdd pair<double, double> #define makeans() freopen("data.out", "w", stdout) using namespace std; const int maxn = 1e7 + 10; const int maxm = 1e6 + 10; const int inf = 0x3f3f3f3f; const ll mod = 1e9 + 7; const int maxblock = sqrt(maxn) + 10; const double eps = 1e-7; const ll INF = 1e16; int res[455] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 110, 111, 153, 200, 221, 370, 371, 407, 500, 702, 1000, 1010, 1011, 1020, 1100, 1101, 1110, 1121, 1122, 1634, 2000, 2322, 4104, 5000, 8208, 9474, 10000, 10010, 10011, 10100, 10101, 10110, 11000, 11001, 11010, 11022, 11100, 11122, 11220, 12012, 12110, 12210, 12320, 14550, 20000, 21120, 21321, 22100, 22110, 36504, 41000, 41500, 41510, 42240, 44364, 50000, 54748, 92727, 93084, 100000, 100010, 100011, 100100, 100101, 100110, 101000, 101001, 101010, 101100, 110000, 110001, 110010, 110100, 111000, 111100, 111110, 112401, 120120, 200000, 210210, 304101, 311100, 322232, 500000, 548834, 647361, 650308, 1000000, 1000010, 1000011, 1000100, 1000101, 1000110, 1001000, 1001001, 1001010, 1001100, 1001102, 1010000, 1010001, 1010010, 1010100, 1011000, 1011100, 1011110, 1011120, 1012230, 1100000, 1100001, 1100010, 1100100, 1101000, 1101012, 1101100, 1101110, 1110000, 1110100, 1110110, 1110120, 1111000, 1111010, 1111100, 1111110, 1112100, 1112121, 1113120, 1121120, 1210020, 1211100, 1212112, 1220211, 1224210, 1300040, 1313100, 1330000, 1741725, 2000000, 2001002, 2021220, 2100120, 2100222, 2132032, 2431000, 3012100, 3104200, 3135000, 3305120, 3334320, 4041042, 4041124, 4210818, 4264064, 5000000, 5242320, 5560625, 6736436, 9800817, 9926315, 10000000, 10000010, 10000011, 10000100, 10000101, 10000110, 10001000, 10001001, 10001010, 10001100, 10010000, 10010001, 10010010, 10010100, 10011000, 10011100, 10011110, 10100000, 10100001, 10100010, 10100100, 10101000, 10101100, 10101110, 10110000, 10110100, 10110110, 10111000, 10111010, 10111100, 10111110, 10201120, 10220210, 11000000, 11000001, 11000010, 11000100, 11001000, 11001100, 11001110, 11001120, 11010000, 11010100, 11010110, 11011000, 11011010, 11011100, 11011110, 11100000, 11100100, 11100110, 11101000, 11101010, 11101100, 11101110, 11101111, 11103400, 11110000, 11110010, 11110100, 11110110, 11111000, 11111010, 11111100, 11210212, 12001220, 12022010, 12120010, 12120120, 12221461, 12310100, 20000000, 20121300, 20221200, 21010220, 21210210, 22001210, 22011100, 22111222, 22144320, 23113020, 24678050, 24678051, 30231012, 32113230, 32321341, 35461510, 41040340, 41223132, 43614000, 44043300, 50000000, 88593477, 88716166, 100000000, 100000010, 100000011, 100000100, 100000101, 100000110, 100001000, 100001001, 100001010, 100001100, 100004220, 100010000, 100010001, 100010010, 100010100, 100011000, 100011100, 100011110, 100011120, 100100000, 100100001, 100100010, 100100100, 100101000, 100101100, 100101110, 100110000, 100110100, 100110110, 100111000, 100111010, 100111100, 100111110, 100201212, 100231410, 101000000, 101000001, 101000010, 101000100, 101001000, 101001100, 101001110, 101010000, 101010100, 101010110, 101011000, 101011010, 101011100, 101011110, 101022222, 101100000, 101100100, 101100110, 101101000, 101101010, 101101100, 101101110, 101110000, 101110010, 101110100, 101110110, 101110200, 101111000, 101111010, 101111100, 101111101, 101124100, 101201121, 101221120, 102111124, 102200022, 102221200, 110000000, 110000001, 110000010, 110000100, 110001000, 110001100, 110001110, 110010000, 110010100, 110010110, 110011000, 110011010, 110011100, 110011110, 110011111, 110011200, 110100000, 110100100, 110100110, 110101000, 110101010, 110101100, 110101110, 110110000, 110110010, 110110100, 110110110, 110111000, 110111010, 110111100, 110210000, 110220102, 110312112, 111000000, 111000100, 111000110, 111001000, 111001010, 111001100, 111001110, 111010000, 111010010, 111010100, 111010110, 111011000, 111011010, 111011100, 111011110, 111100000, 111100010, 111100100, 111100110, 111101000, 111101010, 111101011, 111101100, 111110000, 111110010, 111110100, 111111000, 111111111, 112101000, 112101220, 112212100, 120011220, 120020320, 120102021, 121011020, 121110220, 121221100, 122011120, 122100111, 122220210, 122221000, 131220032, 143336116, 146511208, 172453440, 200000000, 200002100, 201001220, 201021102, 201111000, 210002000, 210033200, 210101202, 210212010, 211013032, 211020552, 211120020, 212101120, 212200212, 212211110, 212220100, 221110120, 232134002, 262302612, 302322200, 320221020, 331110110, 332312022, 340440132, 374816544, 413003304, 420043544, 424202240, 472335975, 500000000, 534494836, 586204332, 634325568, 641102010, 706176030, 733657561, 780036264, 912985153, 1000000000}; ll num[455] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 5, 2, 100, 55, 37, 1, 25, 13, 1, 1, 1, 4, 2, 1000, 505, 337, 60, 550, 367, 370, 59, 33, 1, 125, 18, 8, 8, 1, 1, 10000, 5005, 3337, 5050, 3367, 3370, 5500, 3667, 3670, 167, 3700, 166, 170, 182, 346, 185, 40, 2, 625, 320, 69, 340, 335, 3, 40, 10, 10, 20, 4, 16, 1, 1, 1, 100000, 50005, 33337, 50050, 33367, 33370, 50500, 33667, 33670, 33700, 55000, 36667, 36670, 36700, 37000, 27775, 22222, 27, 924, 3125, 1617, 63, 425, 188, 32, 1, 3, 2, 1000000, 500005, 333337, 500050, 333367, 333370, 500500, 333667, 333670, 333700, 7642, 505000, 336667, 336670, 336700, 337000, 252775, 202222, 7660, 414, 550000, 366667, 366670, 366700, 367000, 8341, 275275, 220222, 370000, 277525, 222022, 8410, 277750, 222202, 222220, 185185, 8425, 4261, 480, 4312, 4690, 9175, 3124, 3153, 73, 70, 300, 304, 1, 15625, 7786, 3940, 8140, 4094, 448, 130, 1300, 166, 38, 40, 132, 82, 82, 1, 7, 64, 54, 7, 4, 1, 1, 10000000, 5000005, 3333337, 5000050, 3333367, 3333370, 5000500, 3333667, 3333670, 3333700, 5005000, 3336667, 3336670, 3336700, 3337000, 2502775, 2002222, 5050000, 3366667, 3366670, 3366700, 3367000, 2525275, 2020222, 3370000, 2527525, 2022022, 2527750, 2022202, 2022220, 1685185, 19808, 13273, 5500000, 3666667, 3666670, 3666700, 3667000, 2750275, 2200222, 42312, 3670000, 2752525, 2202022, 2752750, 2202202, 2202220, 1835185, 3700000, 2775025, 2220022, 2775250, 2220202, 2220220, 1850185, 1585873, 154, 2777500, 2222002, 2222020, 1851685, 2222200, 1851835, 1851850, 14521, 15586, 15613, 23534, 15720, 7, 1805, 78125, 2844, 19728, 27286, 27510, 28573, 42740, 17234, 160, 1695, 1, 1, 2217, 1590, 377, 14, 202, 519, 24, 210, 128, 1, 2, 100000000, 50000005, 33333337, 50000050, 33333367, 33333370, 50000500, 33333667, 33333670, 33333700, 380, 50005000, 33336667, 33336670, 33336700, 33337000, 25002775, 20002222, 193820, 50050000, 33366667, 33366670, 33366700, 33367000, 25025275, 20020222, 33370000, 25027525, 20022022, 25027750, 20022202, 20022220, 16685185, 65108, 355, 50500000, 33666667, 33666670, 33666700, 33667000, 25250275, 20200222, 33670000, 25252525, 20202022, 25252750, 20202202, 20202220, 16835185, 39431, 33700000, 25275025, 20220022, 25275250, 20220202, 20220220, 16850185, 25277500, 20222002, 20222020, 16851685, 195950, 20222200, 16851835, 16851850, 14444443, 385, 98349, 65728, 388, 49878, 49864, 55000000, 36666667, 36666670, 36666700, 36667000, 27500275, 22000222, 36670000, 27502525, 22002022, 27502750, 22002202, 22002220, 18335185, 15715873, 213200, 36700000, 27525025, 22020022, 27525250, 22020202, 22020220, 18350185, 27527500, 22022002, 22022020, 18351685, 22022200, 18351835, 18351850, 214000, 71618, 5326, 37000000, 27750025, 22200022, 27750250, 22200202, 22200220, 18500185, 27752500, 22202002, 22202020, 18501685, 22202200, 18501835, 18501850, 15858730, 27775000, 22220002, 22220020, 18516685, 22220200, 18516835, 15871573, 18516850, 22222000, 18518335, 18518350, 18518500, 12345679, 217250, 72793, 72865, 77980, 5656, 78039, 117715, 78643, 78715, 79228, 118659, 47705, 59620, 3208, 7, 1, 4, 390625, 195124, 130690, 130618, 389750, 204880, 5200, 136518, 136590, 5224, 54, 137180, 137728, 82826, 137710, 103522, 143578, 766, 13, 7300, 14735, 8410, 5439, 412, 2, 708, 139, 538, 1, 256, 1, 4, 4, 62, 7, 7, 4, 1, 1000000000}; int main() { int x, n, m; while (~scanf("%d %d %d", &x, &n, &m)) { int cnt = 0; for (int i = 0; i < 455; i++) { if (num[i] == x && res[i] >= n && res[i] <= m) { printf("%d\n", res[i]); } } printf("\n"); } return 0; }
方法二:看不懂
#include<bits/stdc++.h> using namespace std; #define LL long long LL Pow[11][11]; int a[10000][31]; int x,l,r; bool check(int num) { int dig=0,sc=num; do{ dig++; sc/=10; }while(sc); sc=num; LL DigistSum=0; do{ DigistSum+=Pow[sc%10][dig]; if(DigistSum*x>num)return false; sc/=10; }while(sc); if(DigistSum*x==num)return true; else return false; } void init() { for(int i=1;i<=9;i++) { Pow[i][0]=1; for(int j=1;j<=10;j++) Pow[i][j]=Pow[i][j-1]*i; } a[1][0]=1;a[1][1]=2;a[1][2]=3;a[1][3]=4;a[1][4]=5;a[1][5]=6;a[1][6]=7;a[1][7]=8;a[1][8]=9;a[1][9]=153;a[1][10]=370;a[1][11]=371;a[1][12]=407;a[1][13]=1634;a[1][14]=8208;a[1][15]=9474;a[1][16]=54748;a[1][17]=92727;a[1][18]=93084;a[1][19]=548834;a[1][20]=1741725;a[1][21]=4210818;a[1][22]=9800817;a[1][23]=9926315;a[1][24]=24678050;a[1][25]=24678051;a[1][26]=88593477;a[1][27]=146511208;a[1][28]=472335975;a[1][29]=534494836;a[1][30]=912985153;a[2][0]=50;a[2][1]=702;a[2][2]=14550;a[2][3]=650308;a[2][4]=88716166;a[2][5]=374816544;a[3][0]=36504;a[3][1]=647361;a[4][0]=500;a[4][1]=44364;a[4][2]=6736436;a[4][3]=172453440;a[4][4]=586204332;a[4][5]=634325568;a[4][6]=780036264;a[5][0]=20;a[7][0]=4264064;a[7][1]=5560625;a[7][2]=12221461;a[7][3]=143336116;a[7][4]=706176030;a[7][5]=733657561;a[8][0]=4104;a[8][1]=5000;a[10][0]=10;a[10][1]=41500;a[10][2]=41510;a[13][0]=221;a[13][1]=262302612;a[14][0]=35461510;a[16][0]=50000;a[18][0]=2322;a[20][0]=42240;a[24][0]=43614000;a[25][0]=200;a[27][0]=112401;a[32][0]=500000;a[33][0]=1122;a[37][0]=111;a[38][0]=3135000;a[40][0]=12320;a[40][1]=41000;a[40][2]=3305120;a[54][0]=5242320;a[54][1]=211020552;a[55][0]=110;a[59][0]=1121;a[60][0]=1020;a[62][0]=641102010;a[63][0]=304101;a[64][0]=5000000;a[69][0]=21321;a[70][0]=1300040;a[73][0]=1224210;a[82][0]=4041042;a[82][1]=4041124;a[100][0]=100;a[125][0]=2000;a[128][0]=50000000;a[130][0]=2431000;a[132][0]=3334320;a[139][0]=420043544;a[154][0]=11103400;a[160][0]=22144320;a[166][0]=11122;a[166][1]=3104200;a[167][0]=11022;a[170][0]=11220;a[182][0]=12012;a[185][0]=12210;a[188][0]=322232;a[202][0]=41040340;a[210][0]=44043300;a[256][0]=500000000;a[300][0]=1313100;a[304][0]=1330000;a[320][0]=21120;a[335][0]=22110;a[337][0]=1011;a[340][0]=22100;a[346][0]=12110;a[355][0]=100231410;a[367][0]=1101;a[370][0]=1110;a[377][0]=32321341;a[380][0]=100004220;a[385][0]=101124100;a[388][0]=102111124;a[412][0]=340440132;a[414][0]=1012230;a[425][0]=311100;a[448][0]=2132032;a[480][0]=1113120;a[505][0]=1010;a[519][0]=41223132;a[538][0]=424202240;a[550][0]=1100;a[625][0]=20000;a[708][0]=413003304;a[766][0]=232134002;a[924][0]=120120; a[1000][0]=1000;a[1300][0]=3012100;a[1590][0]=32113230;a[1617][0]=210210;a[1695][0]=23113020;a[1805][0]=12310100;a[2217][0]=30231012;a[2844][0]=20121300;a[3124][0]=1212112;a[3125][0]=200000;a[3153][0]=1220211;a[3208][0]=131220032;a[3337][0]=10011;a[3367][0]=10101;a[3370][0]=10110;a[3667][0]=11001;a[3670][0]=11010;a[3700][0]=11100;a[3940][0]=2021220;a[4094][0]=2100222;a[4261][0]=1112121;a[4312][0]=1121120;a[4690][0]=1210020;a[5005][0]=10010;a[5050][0]=10100;a[5200][0]=210033200;a[5224][0]=211013032;a[5326][0]=110312112;a[5439][0]=332312022;a[5500][0]=11000;a[5656][0]=120020320;a[7300][0]=302322200;a[7642][0]=1001102;a[7660][0]=1011120;a[7786][0]=2001002;a[8140][0]=2100120;a[8341][0]=1101012;a[8410][0]=1110120;a[8410][1]=331110110;a[8425][0]=1112100;a[9175][0]=1211100; } int main() { init(); while(scanf("%d",&x)!=EOF) { scanf("%d%d",&l,&r); if(x>=10000) { if(l%x==0); else l=l+x-l%x; for(int i=l;i<=r;i+=x) { if(check(i))printf("%d\n",i); } } else { for(int i=0;i<=30;i++) if(a[x][i]>=l&&a[x][i]<=r) printf("%d\n",a[x][i]); } printf("\n"); } return 0; }