AtCoder Beginner Contest 106 ABCD
A - Garden
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.
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 8 int main(){ 9 int a,b; 10 cin >> a >> b; 11 printf("%d\n",a*b-(a+b-1)); 12 return 0; 13 }
B - 105
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.
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 8 int main(){ 9 int n; 10 cin >> n; 11 int ans = 0; 12 for(int i = 105; i <= n; i ++) { 13 if(i&1) { 14 int cnt = 0; 15 for(int j = 1; j <= n; j ++) if(i%j==0) cnt++; 16 if(cnt == 8) ans++; 17 } 18 } 19 cout << ans << endl; 20 return 0; 21 }
C - To Infinity
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 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 const int N = 110; 8 char s[N]; 9 int main(){ 10 ll k, l = 0; 11 cin >> s >> k; 12 ll len = strlen(s); 13 while(l < len && s[l] == '1') l ++; 14 if(l+1 <= k) printf("%c\n",s[l]); 15 else printf("1\n"); 16 return 0; 17 }
D - AtCoder Express 2
Problem Statement
In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east. A company called AtCoder Express possesses M trains, and the train i runs from City Li to City Ri (it is possible that Li=Ri). Takahashi the king is interested in the following Q matters:
- The number of the trains that runs strictly within the section from City pi to City qi, that is, the number of trains j such that pi≤Lj and Rj≤qi.
Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.
Constraints
- N is an integer between 1 and 500 (inclusive).
- M is an integer between 1 and 200 000 (inclusive).
- Q is an integer between 1 and 100 000 (inclusive).
- 1≤Li≤Ri≤N (1≤i≤M)
- 1≤pi≤qi≤N (1≤i≤Q)
Input
Input is given from Standard Input in the following format:
N M Q L1 R1 L2 R2 : LM RM p1 q1 p2 q2 : pQ qQ
Output
Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City pi to City qi.
Sample Input 1
2 3 1 1 1 1 2 2 2 1 2
Sample Output 1
3
As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.
Sample Input 2
10 3 2 1 5 2 8 7 10 1 7 3 10
Sample Output 2
1 1
The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1. The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.
Sample Input 3
10 10 10 1 6 2 9 4 5 4 7 4 7 5 8 6 6 6 7 7 9 10 10 1 8 1 9 1 10 2 8 2 9 2 10 3 8 3 9 3 10 1 10
Sample Output 3
7 9 10 6 8 9 6 7 8 10
可以用二维树状数组来做。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 #define lowbit(x) x&(-x) 7 using namespace std; 8 const int MAX = 1010; 9 int c[MAX][MAX]; 10 void add(int x,int y,int k){ 11 for(int i = x; i < MAX; i += lowbit(i)){ 12 for(int j = y; j < MAX; j += lowbit(j)){ 13 c[i][j] += k; 14 } 15 } 16 } 17 int query(int x, int y){ 18 int sum = 0; 19 for(int i = x; i > 0; i -= lowbit(i)){ 20 for(int j = y; j > 0; j -= lowbit(j)){ 21 sum += c[i][j]; 22 } 23 } 24 return sum; 25 } 26 int main(){ 27 int n, m, q, l, r; 28 cin >> n >> m >> q; 29 for(int i = 1; i <= m; i ++) { 30 cin >> l >> r; 31 add(l,r,1); 32 } 33 while(q--) { 34 cin >> l >> r; 35 printf("%d\n",query(r,r)-query(l-1,r)-query(r,l-1)+query(l-1,l-1)); 36 } 37 return 0; 38 }