Vampire Numbers - UVALive 5779 暴力打表

来源:互联网 发布:算法设计与分析基础 3 编辑:程序博客网 时间:2024/05/21 14:45

The number 1827 is an interesting number, because 1827=21*87, and all of the same digits appear on both sides of the `='. The number 136948 has the same property: 136948=146*938.

Such numbers are called Vampire Numbers. More precisely, a number v is a Vampire Number if it has a pair of factors, a and b, where a*b = v, and together, a and b have exactly the same digits, in exactly the same quantities, as v. None of the numbers va or b can have leading zeros. The mathematical definition says thatv should have an even number of digits and that a and b should have the same number of digits, but for the purposes of this problem, we'll relax that requirement, and allow a and b to have differing numbers of digits, and v to have any number of digits. Here are some more examples:

126 = 6 * 2110251 = 51 * 201702189 = 9 * 7802129632 = 32 * 926

Given a number X, find the smallest Vampire Number which is greater than or equal to X.

Input 

There will be several test cases in the input. Each test case will consist of a single line containing a single integer X ( 10$ \le$X$ \le$1, 000, 000). The input will end with a line with a single `0'.

Output 

For each test case, output a single integer on its own line, which is the smallest Vampire Number which is greater than or equal to X. Output no extra spaces, and do not separate answers with blank lines.

Sample Input 

1012612750000

Sample Output 

1261261536880

题意:一个数能够拆成任意两个数,使其成乘积等于这个数,找到不小于给定数的最大数。

思路:暴力打表。

打表代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>typedef long long ll;using namespace std;int vis[10],len,num,q;int c[10],p[10],ans[1000010],ret=0;bool flag;void solve(int sl){ if(sl==len)  { int num1=0,num2=0;    for(int i=2;i<len;i++)    { num1=0;num2=0;      for(int j=i;j>=1;j--)       num1=num1*10+p[j];      for(int j=len;j>i;j--)        num2=num2*10+p[j];      if(num1*num2==num)        flag=true;    }  }  else  { for(int i=1;i<=len;i++)     if(vis[i]==0)     { vis[i]=1;       p[sl+1]=c[i];       solve(sl+1);       vis[i]=0;     }  }}int main(){ int i,j,k;  for(i=10;i<=1010000;i++)  { memset(vis,0,sizeof(vis));    len=0;num=i;    q=i;    while(q)    { k=q%10;      c[++len]=k;      q/=10;    }    flag=false;    solve(0);    if(flag)    { printf("%d\n",i);      ans[++ret]=i;    }  }  for(i=1;i<=ret;i++)   printf("%d,",ans[i]);  printf("num %d\n",ret);}

AC代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>typedef long long ll;using namespace std;int num[604]={126,153,688,1206,1255,1260,1395,1435,1503,1530,1827,2187,3159,3784,6880,10251,10255,10426,10521,10525,10575,11259,11844,11848,12006,12060,12384,12505,12546,12550,12595,12600,12762,12843,12955,12964,13243,13545,13950,14035,14350,15003,15030,15246,15300,15435,15624,15795,16272,17325,17428,17437,17482,18225,18265,18270,19026,19215,21375,21586,21753,21870,25105,25375,25474,25510,28476,29632,31509,31590,33655,33696,36855,37840,37845,39784,41665,42898,44676,45684,45760,45864,47538,48672,49855,51759,52168,53865,56295,56875,62968,63895,67149,67392,67950,68800,71199,78975,100255,100525,101299,102051,102505,102510,102541,102550,102595,102942,102955,103968,104026,104260,104368,105021,105025,105075,105210,105250,105264,105295,105723,105750,107163,107329,108126,108135,108216,108612,108864,109525,110758,112468,112509,112590,114268,115672,115699,116478,116496,116725,116928,117067,118408,118440,118480,118575,118926,119848,120006,120060,120384,120600,120762,120843,121086,121576,121815,122746,122764,123084,123354,123538,123840,123894,124483,124488,124542,124978,125005,125050,125095,125248,125433,125460,125500,125950,125995,126000,126027,126108,126846,127417,127620,128403,128430,128943,129438,129505,129514,129550,129564,129595,129640,129775,129955,131242,132430,132565,132615,132655,133245,134275,134725,135045,135450,135828,135837,136525,136854,136948,138784,139500,139824,140035,140350,141345,142978,143500,143739,143793,145273,145314,145345,145683,146137,146520,146952,149364,149782,150003,150030,150246,150300,150435,150624,150826,152271,152406,152460,152608,152685,152946,153000,153436,154350,155277,156024,156240,156289,156325,156915,157950,158193,162072,162526,162720,162976,163255,163795,163854,163944,164583,165208,168520,171598,172246,172386,172822,173250,173925,174028,174082,174208,174280,174298,174370,174793,174802,174820,174982,175329,176215,178294,178942,179325,179428,179482,180225,180297,180621,182065,182250,182650,182700,182974,184126,186624,187029,189702,189742,190260,190827,191205,192150,192375,192685,192717,193257,193945,194229,197428,197482,197725,201852,205785,207391,208624,210375,210681,210753,211896,212868,213075,213466,213750,213759,214506,215086,215424,215455,215860,216733,217503,217530,217638,217854,218488,218700,223524,226498,226872,226876,227448,229648,231579,231673,233896,236754,236758,236925,238968,241506,241564,243175,245182,245448,246150,246928,250105,250510,251005,251050,251095,251896,253750,254740,255010,255100,256315,256410,256414,258795,259510,260338,261378,261783,262984,263074,263155,263736,267034,268398,279328,281736,283198,283648,284598,284760,285376,286416,286974,287356,289674,291375,291753,293625,295105,295510,296320,297463,297832,304717,307183,312475,312565,312655,312975,314199,314743,315009,315090,315490,315594,315625,315900,316255,319059,319536,325615,326155,326452,328419,328864,329346,329656,336195,336550,336960,338296,341284,341653,342688,346288,346725,346968,347913,352966,355995,361989,362992,365638,368104,368550,368784,369189,371893,373864,375156,375615,376992,378400,378418,378450,381429,384912,384925,386415,390847,392566,393246,393417,394875,397840,399784,404932,404968,414895,415575,416065,416259,416650,416988,419287,428980,429664,435784,439582,442975,446760,446976,447916,449676,449955,450688,451768,456840,457168,457600,458640,462672,465088,465984,468535,475380,475893,476892,486720,488592,489159,489955,490176,491688,493857,495328,497682,498550,515907,516879,517509,517590,519745,520168,520816,521608,521680,526792,529672,530379,531297,535968,536539,538650,549765,559188,562950,564912,567648,568750,571648,573768,588676,611793,611878,612598,614965,617728,618759,623758,629680,632875,638950,649638,661288,665919,667876,671409,671490,671944,673920,678892,679500,687919,688000,692712,697248,702189,702918,710496,711099,711909,711990,715959,719199,729688,736695,738468,741928,769792,773896,778936,782896,785295,789250,789525,789750,791289,792585,794088,798682,809919,809937,809964,815958,829696,841995,859968,899019,936985,939658,960988,1000255,1000525,1002501,1002505,1002550,1002595,1002955,1004251,1005025,1005201,1005250,1005295,1008126,1009525};int main(){ int n,i;  while(~scanf("%d",&n) && n)  { for(i=0;i<=604;i++)     if(n<=num[i])     { printf("%d\n",num[i]);       break;     }  }}




0 0
原创粉丝点击