Time Limit: 1000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
After Maryanna found the treasure buried by 27 pilots in a secret cave, she wanted to leave there immediately. Unfortunately, finding the door closed because of the overweight treasure she carried, she had to find out the password of the lock. She remembered someone had told her the password is an N-digit natural decimal integer (without extra leading zeroes), and the N least significant digits of any positive integral power of that integer is same as itself. She, being a smart girl, came up with all the possible answers within 1 minute. After a few times of tries, she escaped from that cave successfully. To show your intelligence you may solve the same task with your computer within only 10 seconds!
Input
The first line contains T ( T1000), the number of test cases. T lines follow, each contains a single positive integer N (N500).
Output
For each test case, output a single line, which should begin with the case number counting from 1, followed by all the possible passwords sorted in increasing order. If no such passwords exist, output `Impossible' instead. See the sample for more format details.
Sample Input
2 2 1
Sample Output
Case #1: 25 76 Case #2: 0 1 5 6
这是java代码(本来测试不超时,不知为啥)
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static boolean judg(BigInteger a) //判断a是否是符合条件的数字
{
/*
如果一个数abc^2=****abc===>则abc*(abc-1)肯定是****000;
*/
int l=a.toString().length();、l为a的位数
BigInteger as=a.multiply(a.subtract(BigInteger.ONE));
BigInteger n=BigInteger.TEN.pow(l);
/*
String s=as.toString();
int len1=n.toString().length();len1--;
for(int i=s.length()-1;i>s.length()-1-len1;i--){if(s.charAt(i)!='0')return false;}
return true;
*/
if (as.mod(n).equals(BigInteger.ZERO))return true;
else return false;
}
public static void main(String args[])
{
double ti1=(System.currentTimeMillis());
BigInteger ans [][] = new BigInteger[502][2];
BigInteger a= new BigInteger("5");
BigInteger b= new BigInteger("6");
BigInteger c,aa,bb,n;
int j;
//String s1="33676827594484243897647456005000654391916198809258469939943944255181290307214900224081949924583571472291837988649753193941836723828323234739062471943155785513806039500165527193278093329582759905765533802187573309212464055383301491935363862833615950970780658118090418340475522138153859087121701561568296751826571113427262336853480895011970552039185326239496042803106285328198624380944537003185235736096046992680891830197061490109937833490419136188999442576576769103890995893380022607743740081787109376";
// String s2="66323172405515756102352543994999345608083801190741530060056055744818709692785099775918050075416428527708162011350246806058163276171676765260937528056844214486193960499834472806721906670417240094234466197812426690787535944616698508064636137166384049029219341881909581659524477861846140912878298438431703248173428886572737663146519104988029447960814673760503957196893714671801375619055462996814764263903953007319108169802938509890062166509580863811000557423423230896109004106619977392256259918212890625";
int len1,len2;
boolean sa,sb;
for (int ia=1;ia<=500;ia++) ///位数 pow(10 ,ia)
{
/*
如果一个数abc符合条件,根据规律,下一个数应该在0abc----9abc之间
*/
sa=false;
sb=false;
n=BigInteger.TEN.pow(ia);
aa=a;
bb=b;
for (j=0;j<10;j++)
{
aa=aa.add(n);
if (judg(aa))
{
sa=true;
break;
}
}
for (j=0;j<10;j++)
{
bb=bb.add(n);
if (judg(bb))
{
sb=true;
break;
}
}
if (aa.compareTo(n.multiply(BigInteger.TEN))==1)sa=false;
if (bb.compareTo(n.multiply(BigInteger.TEN))==1)sb=false;
if (sa)a=aa;
if (sb)b=bb;
if (sa&&sb)
{
//cout<<min(a,b)<<" "<<max(a,b)<<endl;
if(a.compareTo(b)==1)
{
ans[ia][0]=b;ans[ia][1]=a;
}
else
{
ans[ia][0]=a;ans[ia][1]=b;
}
}
else
{
if (sa){
//cout<<a<<endl;
ans[ia][0]=a;
}
if (sb){
ans[ia][0]=b;
}
}
System.out.println(ans[ia][0]+" "+ans[ia][1]);
/* System.out.println(ans[ia][0]+" "+ans[ia][1]);
System.out.println(ans[ia][0]+" "+ans[ia][1]);
System.out.println(ans[ia][0]+" "+ans[ia][1]);*/
}
double ti2=(System.currentTimeMillis());
System.out.println(ti2-ti1);
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int in;
int t=1;
while(M>0)
{
M--;
in=scanner.nextInt();
System.out.print("Case #"+(t++)+": ");
if(in==1)
{
System.out.println("0 1 5 6");continue;
}
if(ans[in-1][0]!=null)
{
System.out.print(ans[in-1][0]+" ");
if(ans[in-1][1]!=null)System.out.print(ans[in-1][1]);
System.out.println();
continue;
}
System.out.println("Impossible");
}
}
}
呵呵,这道题真是有意思使用java一直超时,一直没好办法,最后还是怪才xcy想到把500位的两个数先找出来,然后倒着往外数。。(很牛逼)
28ms水过!
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
int len1,len2;
char a[1000]={"33676827594484243897647456005000654391916198809258469939943944255181290307214900224081949924583571472291837988649753193941836723828323234739062471943155785513806039500165527193278093329582759905765533802187573309212464055383301491935363862833615950970780658118090418340475522138153859087121701561568296751826571113427262336853480895011970552039185326239496042803106285328198624380944537003185235736096046992680891830197061490109937833490419136188999442576576769103890995893380022607743740081787109376"};
char b[1000]={"66323172405515756102352543994999345608083801190741530060056055744818709692785099775918050075416428527708162011350246806058163276171676765260937528056844214486193960499834472806721906670417240094234466197812426690787535944616698508064636137166384049029219341881909581659524477861846140912878298438431703248173428886572737663146519104988029447960814673760503957196893714671801375619055462996814764263903953007319108169802938509890062166509580863811000557423423230896109004106619977392256259918212890625"};
len1=strlen(a);
len2=strlen(b);
int t,n;
cin>>t;
int g=1;
while(t--)
{
cin>>n;
cout<<"Case #"<<(g++)<<": ";
if(n==1){
cout<<"0 1 5 6"<<endl;continue;
}
if(a[len1-n]==b[len2-n]&&a[len1-n]=='0')
{cout<<"Impossible"<<endl;
continue;
}
if(a[len1-n]>b[len2-n])
{
if(b[len2-n]!='0')
{
for(int i=500-n;i<500;i++)cout<<b[i];
cout<<" ";
}
for(int i=500-n;i<500;i++)cout<<a[i];
cout<<endl;
}
else
{
if(a[len2-n]!='0')
{
for(int i=500-n;i<500;i++)cout<<a[i];
cout<<" ";
}
for(int i=500-n;i<500;i++)cout<<b[i];
cout<<endl;
}
}
return 0;
}