CodeForces 149B - Martian Clock
2013-09-09 14:59
302 查看
B. Martian Clock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations
are forbidden. It was very unfair to Petya as he adored adventure stories that featured lasers and robots.
Today Petya is watching a shocking blockbuster about the Martians called "R2:D2". What can "R2:D2" possibly mean? It might be the Martian time represented in the Martian numeral system. Petya knows that time on Mars is counted just like on the Earth (that is,
there are 24 hours and each hour has 60 minutes). The time is written
as "a:b",
where the string a stands for the number of hours (from 0 to23 inclusive),
and string b stands for the number of minutes (from 0 to 59 inclusive).
The only thing Petya doesn't know is in what numeral system the Martian time is written.
Your task is to print the radixes of all numeral system which can contain the time "a:b".
Input
The first line contains a single string as "a:b"
(without the quotes). There a is a non-empty string, consisting of numbers and uppercase Latin letters. String a shows
the number of hours. String b is a non-empty string that consists of numbers and uppercase Latin letters. String b shows
the number of minutes. The lengths of strings a and b are
from 1 to 5 characters, inclusive. Please note that strings aand b can
have leading zeroes that do not influence the result in any way (for example, string "008:1" in decimal notation denotes correctly written time).
We consider characters 0, 1, ..., 9 as
denoting the corresponding digits of the number's representation in some numeral system, and characters A, B,
..., Z correspond to numbers 10, 11,
..., 35.
Output
Print the radixes of the numeral systems that can represent the time "a:b"
in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "a:b",
print the single integer 0. If there are infinitely many numeral systems that can represent the time "a:b",
print the single integer -1.
Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible.
Sample test(s)
input
output
input
output
input
output
Note
Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6,
represented in the ternary numeral system or as time 17:32 in hexadecimal system.
Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 numeral notation.
There the given string represents time 32:14 that isn't a correct time.
Let's consider the third sample. String "000B:00001" can be perceived as a correct time in the infinite number of numeral systems. If you need an example, you
can take any numeral system with radix no less than 12.
===========================
题目问的就是a:b形式的时间在哪几种进制下a b能满足 在十进制下a属于[0,23],b属于[0,59]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations
are forbidden. It was very unfair to Petya as he adored adventure stories that featured lasers and robots.
Today Petya is watching a shocking blockbuster about the Martians called "R2:D2". What can "R2:D2" possibly mean? It might be the Martian time represented in the Martian numeral system. Petya knows that time on Mars is counted just like on the Earth (that is,
there are 24 hours and each hour has 60 minutes). The time is written
as "a:b",
where the string a stands for the number of hours (from 0 to23 inclusive),
and string b stands for the number of minutes (from 0 to 59 inclusive).
The only thing Petya doesn't know is in what numeral system the Martian time is written.
Your task is to print the radixes of all numeral system which can contain the time "a:b".
Input
The first line contains a single string as "a:b"
(without the quotes). There a is a non-empty string, consisting of numbers and uppercase Latin letters. String a shows
the number of hours. String b is a non-empty string that consists of numbers and uppercase Latin letters. String b shows
the number of minutes. The lengths of strings a and b are
from 1 to 5 characters, inclusive. Please note that strings aand b can
have leading zeroes that do not influence the result in any way (for example, string "008:1" in decimal notation denotes correctly written time).
We consider characters 0, 1, ..., 9 as
denoting the corresponding digits of the number's representation in some numeral system, and characters A, B,
..., Z correspond to numbers 10, 11,
..., 35.
Output
Print the radixes of the numeral systems that can represent the time "a:b"
in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "a:b",
print the single integer 0. If there are infinitely many numeral systems that can represent the time "a:b",
print the single integer -1.
Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible.
Sample test(s)
input
11:20
output
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
input
2A:13
output
0
input
000B:00001
output
-1
Note
Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6,
represented in the ternary numeral system or as time 17:32 in hexadecimal system.
Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 numeral notation.
There the given string represents time 32:14 that isn't a correct time.
Let's consider the third sample. String "000B:00001" can be perceived as a correct time in the infinite number of numeral systems. If you need an example, you
can take any numeral system with radix no less than 12.
===========================
题目问的就是a:b形式的时间在哪几种进制下a b能满足 在十进制下a属于[0,23],b属于[0,59]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; char str[22]; int j=0,k=0,p,a[11],b[11]; bool check(int x) { //cout<<"x="<<x<<endl; int suma=0,sumb=0; for(int i=0;i<j;i++) if(a[i]>=x) return 0; for(int i=0;i<k;i++) if(b[i]>=x) return 0; for(int i=0;i<j;i++) { int temp=1; for(int ii=i+1;ii<j;ii++) { temp*=x; } suma+=a[i]*temp; //cout<<"suma="<<suma<<endl; if(suma>=24) return 0; } for(int i=0;i<k;i++) { int temp=1; for(int ii=i+1;ii<k;ii++) { temp*=x; } sumb+=b[i]*temp; //cout<<"sumb="<<sumb<<endl; if(sumb>=60) return 0; } return 1; } int main() { int ok=0,flag=0; cin>>str; for(int i=0;i<strlen(str);i++) if(str[i]==':') p=i; for(int i=0;i<p;i++) { while(str[i]=='0'&&!flag) i++; if(i==p) break; flag=1; if(str[i]>='A'&&str[i]<='Z') a[j]=str[i]-'A'+10; else a[j]=str[i]-'0'; j++; } flag=0; for(int i=p+1;i<strlen(str);i++) { while(str[i]=='0'&&!flag) i++; if(i==p) break; flag=1; if(str[i]>='A'&&str[i]<='Z') b[k]=str[i]-'A'+10; else b[k]=str[i]-'0'; k++; } /*for(int i=0;i<j;i++) cout<<a[i]; cout<<endl; for(int i=0;i<k;i++) cout<<b[i]; cout<<endl;*/ if((j==0||(j==1&&a[0]<24))&&(k==0||k==1)) { cout<<"-1"<<endl; ok=1; } else { for(int i=2;i<=60;i++) { if(check(i)) { ok=1; cout<<i<<" "; } } if(ok) cout<<endl; } if(!ok) cout<<"0"<<endl; return 0; }
相关文章推荐
- CodeForces - 149B Martian Clock (模拟进制)
- CodeForces - 369E Valera and Queries 离线+树状数组
- codeforces 546D Soldier and Number Game
- Codeforces 590B Chip 'n Dale Rescue Rangers(二分)
- Codeforces 316D2
- 浙工大姗姗杯round2 CodeForces 116B Little Pigs and Wolves
- CodeForces 768 D Jon and Orbs (概率dp)
- CodeForces 91B Queue (线段树单点操作)
- codeforces 602B
- 【CodeForces】[25A]IQ test
- 【Codeforces 631 C Report】+ 单调栈
- codeforces 722C Destroying Array 并查集带权
- 【codeforces 897C】Nephren gives a riddle (递归)
- Codeforces 464A No to Palindromes!(构造)
- CodeForces 15C Industiral Nim Nim游戏
- CodeForces 447B DZY Loves Strings
- [边双连通分量 缩点 虚树 欧拉序列LCA] Codeforces 639F VK Cup 2016 - Round 1 F. Bear and Chemistry
- Codeforces 825 B Five-In-a-Row
- CodeForces - 892C Pride
- Codeforces 464B. Restore Cube