A - Garden
Time limit : 2sec / Memory limit : 1000MB
Score: 100 points
Problem Statement
There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)
What is the area of this yard excluding the roads? Find it.
Note
It can be proved that the positions of the roads do not affect the area.
Constraints
- A is an integer between 2 and 100 (inclusive).
- B is an integer between 2 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
A B
Output
Print the area of this yard excluding the roads (in square yards).
Sample Input 1
2 2
Sample Output 1
1
In this case, the area is 1 square yard.
Sample Input 2
5 7
Sample Output 2
24
In this case, the area is 24 square yards.
容斥定理。
代码:
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); System.out.println(a * b - a - b + 1); } }
B - 105
Time limit : 2sec / Memory limit : 1000MB
Score: 200 points
Problem Statement
The number 105 is quite special - it is odd but still it has eight divisors. Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?
Constraints
- N is an integer between 1 and 200 (inclusive).
Input
Input is given from Standard Input in the following format:
N
Output
Print the count.
Sample Input 1
105
Sample Output 1
1
Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.
Sample Input 2
7
Sample Output 2
0
1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.
代码:
import java.util.*; public class Main { static int get(int a) { int b = 2; for(int i = 3;i <= a / 3;i += 2) { if(a % i == 0) { b ++; } } return b; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int m = 0; if(a >= 105)m ++; for(int i = 107;i <= a;i += 2) { if(get(i) == 8)m ++; } System.out.println(m); } }
C - To Infinity
Time limit : 2sec / Memory limit : 1000MB
Score: 300 points
Problem Statement
Mr. Infinity has a string S consisting of digits from 1
to 9
. Each time the date changes, this string changes as follows:
- Each occurrence of
2
in S is replaced with22
. Similarly, each3
becomes333
,4
becomes4444
,5
becomes55555
,6
becomes666666
,7
becomes7777777
,8
becomes88888888
and9
becomes999999999
.1
remains as1
.
For example, if S is 1324
, it becomes 1333224444
the next day, and it becomes 133333333322224444444444444444
the day after next. You are interested in what the string looks like after 5×1015 days. What is the K-th character from the left in the string after 5×1015 days?
Constraints
- S is a string of length between 1 and 100 (inclusive).
- K is an integer between 1 and 1018 (inclusive).
- The length of the string after 5×1015 days is at least K.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the K-th character from the left in Mr. Infinity's string after 5×1015 days.
Sample Input 1
1214 4
Sample Output 1
2
The string S changes as follows:
- Now:
1214
- After one day:
12214444
- After two days:
1222214444444444444444
- After three days:
12222222214444444444444444444444444444444444444444444444444444444444444444
The first five characters in the string after 5×1015 days is 12222
. As K=4, we should print the fourth character, 2
.
Sample Input 2
3 157
Sample Output 2
3
The initial string is 3
. The string after 5×1015 days consists only of 3
.
Sample Input 3
299792458 9460730472580800
Sample Output 3
2
只需要看看开头有几个连续的1即可。
代码:
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); long a = in.nextLong(); int b = 0; for(int i = 0;i < s.length();i ++) { if(s.charAt(i) == '1')b ++; else break; } if(b >= a)System.out.println(1); else System.out.println(s.charAt(b)); } }