google杯邀请赛 Avaricious Maryanna
来源:互联网 发布:苹果电脑解压缩软件 编辑:程序博客网 时间:2024/05/16 11:40
[Submit] [Go Back] [Status]
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
221
Sample Output
Case #1: 25 76Case #2: 0 1 5 6
[Submit] [Go Back] [Status]
这是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;
}
- google杯邀请赛 Avaricious Maryanna
- UVA 12009 - Avaricious Maryanna
- 2011ACM上海邀请赛A题(Avaricious Maryanna)----模拟+大数乘法
- UVA 12009 - Avaricious Maryanna(数论)
- uva 12009 - Avaricious Maryanna(暴力)
- Python语言初学(为机器学习做准备) Avaricious Maryanna
- 2011 Google 杯 上海邀请赛 Juice Extractor
- 2011 Google 杯 上海邀请赛 Juice Extractor
- 2008年Intel杯嵌入式邀请赛赛后总结
- 转会-"中信软件教育杯" 2011 湘潭市第三届邀请赛
- 2016湘潭大学“长城信息杯”程序设计邀请赛总结
- 参加英特尔杯嵌入式邀请赛暨参观英特尔公司总结
- 【SC主题公园杯邀请赛】【cqbzoj1005】三个袋子 如何手残
- xt邀请赛
- 西安邀请赛
- 北京邀请赛
- 上海邀请赛
- 怀化邀请赛
- VB.NET调用SQL Server存储过程
- Java关键字final、static使用总结
- PHP正则表达式-贪婪与懒惰匹配
- c# wince 读取MP3信息
- iPhone/Mac Objective-C内存管理教程和原理剖析(三)@property (retain)和@synthesize的默认实现
- google杯邀请赛 Avaricious Maryanna
- 无法获得数据库'model'上的排他锁 解决办法
- iPhone/Mac Objective-C内存管理教程和原理剖析(四)系统自动创建新的autorelease pool
- 为Windows 2003服务器打造铜墙铁壁
- 网站如何保证网页防篡改
- 关于 Oracle 的数据导入导出及 Sql Loader (sqlldr) 的用法
- 如何配置Win 2003的DNS服务器
- 服务器维护内容
- 服务器,怎么设置才能安全