Skip to content

Commit 65eab00

Browse files
committed
🚀 15-Jan-2021
1 parent ff3387c commit 65eab00

14 files changed

+1104
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive (1 Based indexing). You are guaranteed that after excluding the part of the array remaining array is non empty.
3+
4+
Input
5+
First line of input contains an integer T denoting number of test cases.
6+
For each test case, first line will contain two space separated integers N, Q.
7+
Next line contains N space separated integers denoting array A.
8+
For next Q lines, each line will contain a query denoted by two space separated integers L, R.
9+
Output
10+
For each query, print a single integer representing the answer of that query.
11+
Constraints
12+
Subtask #1: 40 points
13+
14+
2 = T, N = 100, 1 = Q = N, 1 = A[i] = 105
15+
1 = L, R = N and L = R
16+
Subtask #2: 60 points
17+
18+
2 = T, N = 105, 1 = Q = N, 1 = A[i] = 105
19+
1 = L, R = N and L = R
20+
Sum of N over all the test cases will be less than or equal to 106.
21+
Example
22+
Input:
23+
1
24+
3 3
25+
2 6 9
26+
1 1
27+
2 2
28+
2 3
29+
30+
Output:
31+
3
32+
1
33+
2
34+
Explanation
35+
For first query, the remaining part of array will be (6, 9), so answer is 3. For second query, the remaining part of array will be (2, 9), so answer is 1. For third query, the remaining part of array will be (2), so answer is 2.
36+
*/
37+
38+
39+
40+
#include<bits/stdc++.h>
41+
using namespace std;
42+
43+
int gcdd (int a, int b) {
44+
if (a == 0) return b;
45+
return gcdd(b % a, a);
46+
}
47+
48+
int main(){
49+
ios_base::sync_with_stdio(false);
50+
cin.tie(NULL);
51+
52+
int t;
53+
cin >> t;
54+
55+
while (t--) {
56+
int n, q;
57+
cin >> n >> q;
58+
59+
vector <int> v(n);
60+
for (int i = 0; i < n; i++) cin >> v[i];
61+
62+
vector <int> pre(n), suf(n);
63+
64+
pre[0] = v[0];
65+
66+
for (int i = 1; i < n; i++) pre[i] = gcdd(pre[i - 1], v[i]);
67+
68+
suf[n - 1] = v[n - 1];
69+
70+
for (int i = n - 2; i >= 0; i--) suf[i] = gcdd(suf[i + 1], v[i]);
71+
72+
73+
while (q--) {
74+
int l, r;
75+
cin >> l >> r;
76+
77+
l -= 2;
78+
79+
if (l >=0 && r <= n - 1) cout << gcdd(pre[l], suf[r]) << endl;
80+
else if (l < 0) cout << suf[r] << endl;
81+
else cout << pre[l] << endl;
82+
}
83+
}
84+
85+
return 0;
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
You have an array a1,a2,…,an. All ai are positive integers.
3+
4+
In one step you can choose three distinct indices i, j, and k (i?j; i?k; j?k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.
5+
6+
Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?
7+
8+
Input
9+
The first line contains a single integer t (1=t=2000) — the number of test cases.
10+
11+
The first line of each test case contains two integers n and d (3=n=100; 1=d=100) — the number of elements in the array a and the value d.
12+
13+
The second line contains n integers a1,a2,…,an (1=ai=100) — the array a.
14+
15+
Output
16+
For each test case, print YES, if it's possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.
17+
18+
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
19+
20+
Example
21+
inputCopy
22+
3
23+
5 3
24+
2 3 2 5 4
25+
3 4
26+
2 4 4
27+
5 4
28+
2 1 5 3 6
29+
outputCopy
30+
NO
31+
YES
32+
YES
33+
Note
34+
In the first test case, we can prove that we can't make all ai=3.
35+
36+
In the second test case, all ai are already less or equal than d=4.
37+
38+
In the third test case, we can, for example, choose i=5, j=1, k=2 and make a5=a1+a2=2+1=3. Array a will become [2,1,5,3,3].
39+
40+
After that we can make a3=a5+a2=3+1=4. Array will become [2,1,4,3,3] and all elements are less or equal than d=4.
41+
*/
42+
43+
44+
45+
46+
47+
48+
#include<bits/stdc++.h>
49+
using namespace std;
50+
51+
52+
int main(){
53+
ios_base::sync_with_stdio(false);
54+
cin.tie(NULL);
55+
56+
int t;
57+
cin >> t;
58+
59+
while (t--) {
60+
int n, d;
61+
cin >> n >> d;
62+
63+
vector <int> v(n);
64+
65+
for (int i = 0; i < n; i++) cin >> v[i];
66+
67+
sort(v.begin(), v.end());
68+
69+
if (v[n - 1] <= d) cout << "YES" << endl;
70+
else {
71+
int tmp = v[0] + v[1];
72+
if (tmp <= d) cout << "YES" << endl;
73+
else cout << "NO" << endl;
74+
}
75+
}
76+
77+
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Let's define a multiplication operation between a string a and a positive integer x: a·x is the string that is a result of writing x copies of a one after another. For example, "abc" · 2 = "abcabc", "a" · 5 = "aaaaa".
3+
4+
A string a is divisible by another string b if there exists an integer x such that b·x=a. For example, "abababab" is divisible by "ab", but is not divisible by "ababab" or "aa".
5+
6+
LCM of two strings s and t (defined as LCM(s,t)) is the shortest non-empty string that is divisible by both s and t.
7+
8+
You are given two strings s and t. Find LCM(s,t) or report that it does not exist. It can be shown that if LCM(s,t) exists, it is unique.
9+
10+
Input
11+
The first line contains one integer q (1=q=2000) — the number of test cases.
12+
13+
Each test case consists of two lines, containing strings s and t (1=|s|,|t|=20). Each character in each of these strings is either 'a' or 'b'.
14+
15+
Output
16+
For each test case, print LCM(s,t) if it exists; otherwise, print -1. It can be shown that if LCM(s,t) exists, it is unique.
17+
18+
Example
19+
inputCopy
20+
3
21+
baba
22+
ba
23+
aa
24+
aaa
25+
aba
26+
ab
27+
outputCopy
28+
baba
29+
aaaaaa
30+
-1
31+
Note
32+
In the first test case, "baba" = "baba" · 1 = "ba" · 2.
33+
34+
In the second test case, "aaaaaa" = "aa" · 3 = "aaa" · 2.
35+
*/
36+
37+
38+
39+
40+
41+
#include<bits/stdc++.h>
42+
using namespace std;
43+
44+
int gcdd (int a, int b) {
45+
if(a == 0) return b;
46+
return gcdd(b % a, a);
47+
}
48+
49+
int main(){
50+
ios_base::sync_with_stdio(false);
51+
cin.tie(NULL);
52+
53+
int q;
54+
cin >> q;
55+
56+
while (q--) {
57+
string s, t;
58+
cin >> s >> t;
59+
60+
int slen = s.length();
61+
int tlen = t.length();
62+
63+
int len = (slen * tlen) / gcdd(slen, tlen);
64+
65+
int sl = len / slen - 1;
66+
67+
string tmp = s;
68+
69+
while (sl--) {
70+
s += tmp;
71+
}
72+
73+
int tl = len / tlen - 1;
74+
75+
tmp = t;
76+
77+
while (tl--) {
78+
t += tmp;
79+
}
80+
81+
if(s == t) cout << s << endl;
82+
else cout << -1 << endl;
83+
84+
85+
}
86+
87+
88+
}
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This winter is so cold in Nvodsk! A group of n friends decided to buy k bottles of a soft drink called "Take-It-Light" to warm up a bit. Each bottle has l milliliters of the drink. Also they bought c limes and cut each of them into d slices. After that they found p grams of salt.
3+
4+
To make a toast, each friend needs nl milliliters of the drink, a slice of lime and np grams of salt. The friends want to make as many toasts as they can, provided they all drink the same amount. How many toasts can each friend make?
5+
6+
Input
7+
The first and only line contains positive integers n, k, l, c, d, p, nl, np, not exceeding 1000 and no less than 1. The numbers are separated by exactly one space.
8+
9+
Output
10+
Print a single integer — the number of toasts each friend can make.
11+
12+
Examples
13+
inputCopy
14+
3 4 5 10 8 100 3 1
15+
outputCopy
16+
2
17+
inputCopy
18+
5 100 10 1 19 90 4 3
19+
outputCopy
20+
3
21+
inputCopy
22+
10 1000 1000 25 23 1 50 1
23+
outputCopy
24+
0
25+
Note
26+
A comment to the first sample:
27+
28+
Overall the friends have 4?*?5?=?20 milliliters of the drink, it is enough to make 20?/?3?=?6 toasts. The limes are enough for 10?*?8?=?80 toasts and the salt is enough for 100?/?1?=?100 toasts. However, there are 3 friends in the group, so the answer is min(6,?80,?100)?/?3?=?2
29+
*/
30+
31+
32+
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int main(){
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
42+
int n, k, l, c, d, p, nl, np;
43+
cin >> n >> k >> l >> c >> d >> p >> nl >> np;
44+
45+
int x = (k * l) / nl;
46+
int y = c * d;
47+
int z = p / np;
48+
49+
cout << min(x, min(y, z)) / n;
50+
51+
52+
return 0;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness.
3+
4+
Input
5+
The first line contains integer n (3?=?n?=?100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.
6+
7+
Output
8+
Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.
9+
10+
Examples
11+
inputCopy
12+
5
13+
2 4 7 8 10
14+
outputCopy
15+
3
16+
inputCopy
17+
4
18+
1 2 1 1
19+
outputCopy
20+
2
21+
22+
*/
23+
24+
25+
26+
27+
#include<bits/stdc++.h>
28+
using namespace std;
29+
30+
31+
int main(){
32+
ios_base::sync_with_stdio(false);
33+
cin.tie(NULL);
34+
35+
int n;
36+
cin >> n;
37+
38+
vector <int> v(n);
39+
for (int i = 0; i < n; i++) cin >> v[i];
40+
41+
int oc = 0, oe = 0;
42+
43+
for (int i = 0; i < 3; i++) {
44+
if(v[i] % 2 == 0) oe++;
45+
else oc++;
46+
}
47+
48+
if(oc > oe) {
49+
for (int i = 0; i < n; i++) {
50+
if (v[i] % 2 == 0) {
51+
cout << i + 1;
52+
break;
53+
}
54+
}
55+
} else {
56+
for (int i = 0; i < n; i++) {
57+
if (v[i] % 2 == 1) {
58+
cout << i + 1;
59+
break;
60+
}
61+
}
62+
}
63+
64+
65+
}

0 commit comments

Comments
 (0)