Skip to content

Commit 62b66ab

Browse files
committed
Solve alot of problems :3
1 parent 98622f5 commit 62b66ab

21 files changed

+696
-0
lines changed

Diff for: CodeForces/110C. Lucky Sum of Digits.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, dp[int(1e6 + 1)][2];
6+
7+
int rec(int sum, bool f) {
8+
if(sum > n)
9+
return 1e9;
10+
11+
if(sum == n)
12+
return 0;
13+
14+
int &ret = dp[sum][f];
15+
if(ret != -1)
16+
return ret;
17+
ret = 0;
18+
19+
ret = min(rec(sum + (!f ? 4 : 7), 0) + 1, rec(sum + (!f ? 4 : 7), 1) + 1);
20+
21+
return ret;
22+
}
23+
24+
void print(int sum, bool f) {
25+
if(sum > n)
26+
return;
27+
28+
if(sum == n)
29+
return;
30+
31+
if(!f)
32+
putchar('4');
33+
else
34+
putchar('7');
35+
36+
int opt = min(rec(sum + (!f ? 4 : 7), 0) + 1, rec(sum + (!f ? 4 : 7), 1) + 1);
37+
38+
if(opt == rec(sum + (!f ? 4 : 7), 0) + 1)
39+
print(sum + (!f ? 4 : 7), 0);
40+
else
41+
print(sum + (!f ? 4 : 7), 1);
42+
}
43+
44+
int main() {
45+
scanf("%d", &n);
46+
memset(dp, -1, sizeof dp);
47+
int mn = min(rec(0, 0), rec(0, 1));
48+
if(mn >= 1e9) {
49+
puts("-1");
50+
return 0;
51+
} else if(mn == rec(0, 0))
52+
print(0, 0);
53+
else
54+
print(0, 1);
55+
puts("");
56+
57+
return 0;
58+
}

Diff for: CodeForces/152C. Pocket Book.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m, freq[101][26], all[101];
6+
char s[101];
7+
8+
int main() {
9+
scanf("%d %d", &n, &m);
10+
for(int i = 0; i < n; ++i) {
11+
scanf("%s", s);
12+
for(int j = 0; j < m; ++j)
13+
++freq[j][s[j] - 'A'];
14+
}
15+
16+
for(int i = 0; i < m; ++i)
17+
for(int j = 0; j < 26; ++j)
18+
all[i] += (freq[i][j] != 0);
19+
20+
long long res = 1;
21+
for(int i = 0; i < m; ++i)
22+
res = (res * (all[i] == 0 ? 1 : all[i])) % int(1e9 + 7);
23+
printf("%lld\n", res);
24+
25+
return 0;
26+
}
27+

Diff for: CodeForces/186C. Plant.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const MOD = 1000000007;
6+
long long n;
7+
8+
long long power(int b, long long p) {
9+
if(p == 0)
10+
return 1;
11+
12+
long long res = power(b, p >> 1);
13+
res = res * res % MOD;
14+
if(p & 1)
15+
res = res * b % MOD;
16+
return res;
17+
}
18+
19+
int main() {
20+
scanf("%lld", &n);
21+
long long r = power(2, n);
22+
printf("%lld\n", (r * (r + 1) / 2) % MOD);
23+
24+
return 0;
25+
}

Diff for: CodeForces/382C. Arithmetic Progression.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, a[int(1e5 + 1)];
6+
set<int> st, all;
7+
8+
int main() {
9+
scanf("%d", &n);
10+
for(int i = 0; i < n; ++i) {
11+
scanf("%d", a + i);
12+
all.insert(a[i]);
13+
}
14+
15+
if(n == 1) {
16+
puts("-1");
17+
return 0;
18+
}
19+
20+
sort(a, a + n);
21+
22+
if(n == 2) {
23+
if(a[0] == a[1])
24+
printf("1\n%d\n", a[0]);
25+
else if((a[1] - a[0] - 1) & 1)
26+
printf("3\n%d %d %d\n", a[0] - (a[1] - a[0]), (a[0] + a[1]) / 2, a[1] + (a[1] - a[0]));
27+
else
28+
printf("2\n%d %d\n", a[0] - (a[1] - a[0]), a[1] + (a[1] - a[0]));
29+
return 0;
30+
}
31+
32+
for(int i = 1; i < n; ++i)
33+
st.insert(a[i] - a[i - 1]);
34+
35+
if(st.size() == 1) {
36+
if(all.size() == 1)
37+
printf("1\n%d\n", a[0]);
38+
else
39+
printf("2\n%d %d\n", a[0] - *st.begin(), a[n - 1] + *st.begin());
40+
} else if(st.size() == 2) {
41+
int mn = *st.begin();
42+
int mx = *(++st.begin());
43+
44+
if(mn + mn == mx) {
45+
int tmp = 0, idx;
46+
for(int i = 1; i < n; ++i)
47+
if(a[i] - a[i - 1] == mx)
48+
++tmp, idx = i;
49+
if(tmp == 1)
50+
printf("1\n%d\n", (a[idx] + a[idx - 1]) / 2);
51+
else
52+
puts("0");
53+
} else {
54+
puts("0");
55+
}
56+
} else {
57+
puts("0");
58+
}
59+
60+
return 0;
61+
}

Diff for: CodeForces/389C. Fox and Box Accumulation.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, a[101];
6+
7+
bool can(int mid) {
8+
for(int i = 0; i < n; ++i)
9+
if(i / mid > a[i])
10+
return false;
11+
return true;
12+
}
13+
14+
int main() {
15+
scanf("%d", &n);
16+
for(int i = 0; i < n; ++i)
17+
scanf("%d", a + i);
18+
sort(a, a + n);
19+
20+
for(int i = 1; i < 101; ++i)
21+
if(can(i)) {
22+
printf("%d\n", i);
23+
return 0;
24+
}
25+
26+
return 0;
27+
}

Diff for: CodeForces/463C. Gargari and Bishops.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 3001;
6+
int n, g[N][N], x, y, xx, yy;
7+
long long dp1[2 * N], dp2[2 * N], sol1 = -1, sol2 = -1;
8+
9+
int main() {
10+
scanf("%d", &n);
11+
for(int i = 1; i <= n; ++i)
12+
for(int j = 1; j <= n; ++j) {
13+
scanf("%d", &g[i][j]);
14+
dp1[i + j] += g[i][j];
15+
dp2[i - j + n] += g[i][j];
16+
}
17+
18+
for(int i = 1; i <= n; ++i)
19+
for(int j = 1; j <= n; ++j) {
20+
long long v = dp1[i + j] + dp2[i - j + n] - g[i][j];
21+
if((i + j) & 1) {
22+
if(v > sol2) {
23+
sol2 = v;
24+
xx = i;
25+
yy = j;
26+
}
27+
} else {
28+
if(v > sol1) {
29+
sol1 = v;
30+
x = i;
31+
y = j;
32+
}
33+
}
34+
}
35+
36+
printf("%lld\n", sol1 + sol2);
37+
printf("%d %d %d %d\n", x, y, xx, yy);
38+
39+
return 0;
40+
}

Diff for: CodeForces/9C. Hexadecimal's Numbers.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
vector<long long> all;
6+
void rec(long long sum, int idx) {
7+
if(idx == 10) {
8+
all.push_back(sum);
9+
return;
10+
}
11+
12+
rec(sum, idx + 1);
13+
rec(sum * 10, idx + 1);
14+
rec(sum * 10 + 1, idx + 1);
15+
}
16+
17+
int main() {
18+
rec(1, 0);
19+
sort(all.begin(), all.end());
20+
all.resize(unique(all.begin(), all.end()) - all.begin());
21+
22+
int n, res = 0;
23+
scanf("%d", &n);
24+
for(int i = 0; i < (int)all.size(); ++i) {
25+
if(all[i] > n)
26+
break;
27+
++res;
28+
}
29+
printf("%d\n", res);
30+
31+
return 0;
32+
}

Diff for: CodeForces/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [1A. Theatre Square](http://codeforces.com/problemset/problem/1/A)
44
- [4A. Watermelon](http://codeforces.com/contest/4/problem/A)
5+
- [9C. Hexadecimal's Numbers](http://codeforces.com/contest/9/problem/C)
56
- [12A. Super Agent](http://codeforces.com/contest/12/problem/A)
67
- [12B. Correct Solution?](http://codeforces.com/contest/12/problem/B)
78
- [14C. Four Segments](http://codeforces.com/contest/14/problem/C)
@@ -38,6 +39,7 @@
3839
- [96A. Football](http://codeforces.com/problemset/problem/96/A)
3940
- [96B. Lucky Numbers (easy)](http://codeforces.com/problemset/problem/96/B)
4041
- [110A. Nearly Lucky Number](http://codeforces.com/problemset/problem/110/A)
42+
- [110C. Lucky Sum of Digits](http://codeforces.com/contest/110/problem/C)
4143
- [112A. Petya and Strings](http://codeforces.com/contest/112/problem/A)
4244
- [115A. Party](http://codeforces.com/problemset/problem/115/A)
4345
- [116A. Tram](http://codeforces.com/problemset/problem/116/A)
@@ -52,10 +54,12 @@
5254
- [141A. Amusing Joke](http://codeforces.com/problemset/problem/141/A)
5355
- [144A. Arrival of the General](http://codeforces.com/contest/144/problem/A)
5456
- [151A. Soft Drinking](http://codeforces.com/problemset/problem/151/A)
57+
- [152C. Pocket Book](http://codeforces.com/contest/152/problem/C)
5558
- [158A. Next Round](http://codeforces.com/problemset/problem/158/A)
5659
- [158B. Taxi](http://codeforces.com/contest/158/problem/B)
5760
- [160A. Twins](http://codeforces.com/problemset/problem/160/A)
5861
- [182D. Common Divisors](http://codeforces.com/contest/182/problem/D)
62+
- [186C. Plant](http://codeforces.com/contest/186/problem/C)
5963
- [192A. Funky Numbers](http://codeforces.com/problemset/problem/192/A)
6064
- [192B. Walking in the Rain](http://codeforces.com/problemset/problem/192/B)
6165
- [200B. Drinks](http://codeforces.com/problemset/problem/200/B)
@@ -101,7 +105,9 @@
101105
- [368A. Sereja and Coat Rack](http://codeforces.com/problemset/problem/368/A)
102106
- [368B. Sereja and Suffixes](http://codeforces.com/problemset/problem/368/B)
103107
- [378A. Playing with Dice](http://codeforces.com/problemset/problem/378/A)
108+
- [382C. Arithmetic Progression](http://codeforces.com/contest/382/problem/C)
104109
- [385A. Bear and Raspberry](http://codeforces.com/problemset/problem/385/A)
110+
- [389C. Fox and Box Accumulation](http://codeforces.com/contest/389/problem/C)
105111
- [393A. Nineteen](http://codeforces.com/problemset/problem/393/A)
106112
- [394A. Counting Sticks](http://codeforces.com/problemset/problem/394/A)
107113
- [401C. Team](http://codeforces.com/contest/401/problem/C)
@@ -125,6 +131,7 @@
125131
- [456C. Boredom](http://codeforces.com/contest/456/problem/C)
126132
- [459A. Pashmak and Garden](http://codeforces.com/problemset/problem/459/A)
127133
- [462A. Appleman and Easy Task](http://codeforces.com/problemset/problem/462/A)
134+
- [463C. Gargari and Bishops](http://codeforces.com/contest/463/problem/C)
128135
- [466A. Cheap Travel](http://codeforces.com/contest/466/problem/A)
129136
- [466C. Number of Ways](http://codeforces.com/contest/466/problem/C)
130137
- [467B. Fedor and New Game](http://codeforces.com/problemset/problem/467/B)

Diff for: SPOJ/ATOMS - Atoms in the Lab.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int t, res;
6+
long long n, k, m, tmp;
7+
8+
int main() {
9+
scanf("%d", &t);
10+
while(t-- != 0) {
11+
scanf("%lld %lld %lld", &n, &k, &m);
12+
13+
if(n > m) {
14+
puts("0");
15+
continue;
16+
}
17+
18+
res = 0;
19+
while(m >= n) {
20+
m /= k;
21+
++res;
22+
}
23+
printf("%d\n", res - 1);
24+
}
25+
26+
return 0;
27+
}

Diff for: SPOJ/COMDIV - Number of common divisors.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1000001;
6+
int t, a, b, gcd;
7+
vector<int> all;
8+
9+
void divisors(int n) {
10+
int sqrtn = sqrt(n);
11+
for(int i = 1; i <= sqrtn; ++i) {
12+
if(n % i == 0) {
13+
++all[n];
14+
if(n / i != i)
15+
++all[n];
16+
}
17+
}
18+
}
19+
20+
int main() {
21+
all.resize(N);
22+
scanf("%d", &t);
23+
while(t-- != 0) {
24+
scanf("%d %d", &a, &b);
25+
gcd = __gcd(a, b);
26+
if(all[gcd] == 0)
27+
divisors(gcd);
28+
printf("%d\n", all[gcd]);
29+
}
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)