Hello everyone, We hope you enjoyed the problems, Here's the editorial of our round, If you noticed any mistake please write it down in the comments, Thanks :D.
If you check all the cases from $$$1$$$ to $$$12$$$, You'll understand that if $$$n$$$ is a divisor of $$$12$$$, Answer is Yes, Otherwise the answer is No.
/*
* ────────────── •✵•✵• ──────────────
* | In The Name of *Allah* |
* ────────────── •✵•✵• ──────────────
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
void solve (){
int n;
cin >> n;
cout << (12 % n == 0 ? "Yes" : "No");
}
int main () {
ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tc = 1;
// cin >> tc;
while (tc --) {
solve();
}
return 0;
}
/* Thanks *Allah* */
The first condition is that $$$c$$$ and $$$d$$$ both must be greater than $$$a$$$ and $$$b$$$. So select two integers in such a way that both have only one common divisor and that must be $$$gcd(a, b)$$$. Now you can select both elements in the following way.
Now if $$$gcd(a,b) = g$$$ then $$$gcd(a+(x×g),b+(y×g))$$$ is also $$$g$$$. where $$$x$$$ and $$$y$$$ are non-zero positive integers.
so best suitable solution is $$$c = (max(a,b)+g)$$$ and $$$d = (max(a,b)+2×g)$$$ because $$$c$$$ and $$$d$$$ need to be distinct.
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
int g = __gcd(a,b);
int maxi = max(a,b);
cout<<maxi+g<<' '<<maxi+2*g<<'\n';
return 0;
}
For this particular problem you can solve it in too many ways but let's discuss $$$O(n^2)$$$ first and then we discuss the $$$O(n)$$$ solution.
$$$O(n^2)$$$ solution:
Traverse through $$$1$$$ to $$$n$$$ and now if you want to count divisors of $$$i$$$ you can find all of them in $$$O(n)$$$ and for this problem constraint is low you can do $$$O(n^2)$$$.
$$$O(n)$$$ solution:
If you want to find total numbers which have number $$$x$$$ in their divisors in the range $$$1$$$ to $$$n$$$ you can see such numbers form an Arithmetic progression like $$$x, 2x , 3x , ... $$$ so it's an arithmetic progression of a difference $$$x$$$ so the total number of terms in this AP is $$$\lfloor \frac{n}{x} \rfloor$$$ and sum of them is $$$\lfloor \frac{n}{x} \rfloor × x$$$
so we just have to traverse through $$$1$$$ to $$$n$$$ and count of all factors of all numbers will be $$$\sum \lfloor \frac{n}{i} \rfloor$$$ and sum all factors will be $$$\sum \lfloor \frac{n}{i} \rfloor × i$$$
// In the name of GOD
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int ans1 = 0, ans2 = 0, counter;
for (int i = 1; i <= n; i++) {
counter = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
ans1 += j;
counter++;
}
}
ans2 += counter;
}
cout << ans2 << ' ' << ans1;
return 0;
}
Maximum sum we can form with a number of length $$$m$$$ is $$$9×m$$$(by placing all nines).so if $$$s$$$ is greater than $$$9×m$$$ there is no answer.
Now to find minimum number:
Traverse from $$$i=m-1$$$ to $$$i=0$$$ and fill every digit.
if $$$i=m-1$$$ then we fill $$$1^{st}$$$ digit and $$$(m-1)$$$ digits are remaining (excluding present digit).
if $$$i=m-2$$$ then we will $$$2^{nd}$$$ digit and $$$(m-2)$$$ digits are remaining(excluding present digit).
so if you are at $$$i$$$ then $$$i$$$ digits are remaining.The maximum possible sum we can form with those remaining $$$i$$$ digits is $$$9×i$$$ and $$$k$$$ is the remaining sum.
hence $$$j=max(0,k - 9×i)$$$ so assign $$$1$$$ to $$$j$$$ if $$$i = m-1$$$ and $$$j = 0$$$ because we should not have leading zeroes and we take $$$j$$$ at that place and subtracting $$$j$$$ from remaining sum $$$k$$$.
Now to find the maximum number:
first, we take as many nines as possible and decrease the sum by $$$9$$$ respectively, and when the sum becomes less than $$$9$$$ we print that digit, and the remaining digits should be zero.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
int main (){
int m, s, i, k;
cin >> m >> s;
if (s < 1 && m > 1 || s > m * 9) cout << -1 << " " << -1 << endl;
else {
for (i = m - 1, k = s; i >= 0; i--) {
int j = max(0, k - 9 * i);
if (j == 0 && i == m - 1 && k) j = 1;
cout << j;
k -= j;
}
cout << ' ';
for (i = m - 1, k = s; i >= 0; i--) {
int j = min(9, k);
cout << j;
k -= j;
}
}
}
// Thanks Allah
source : 103150I — EZ Programming Contest #1 — X-OR XOR Let's figure out what $$$x$$$ should be by going bit by bit in its binary representation. In particular, let $$$a_i, b_i,$$$ and $$$x_i$$$ denote the ith bit from the right in $$$a, b,$$$ and $$$x$$$, respectively. Then for all positive integers $$$i$$$:
- If $$$a_i=0$$$ and $$$b_i=0$$$, $$$x_i$$$ can be $$$0$$$ or $$$1$$$. However, since $$$x≤10^{18}$$$, it is better to set $$$x_i=0$$$ to avoid going over that limit.
- If $$$a_i=0$$$ and $$$b_i=1$$$, no such $$$x_i$$$ exists, and the answer is $$$−1$$$.
- If $$$a_i=1$$$ and $$$b_i=0$$$, $$$x_i=1$$$.
- If $$$a_i=1$$$ and $$$b_i=1$$$, $$$x_i=0$$$. Therefore, by going bit by bit, we can construct such an $$$x$$$ or report whether no such $$$x$$$ exists. This runs in $$$O(log N)$$$ per test case.
There is also a simpler solution not involving going bit by bit, which uses the same proof as above: just set $$$x=$$$ $$$a$$$ $$$XOR$$$ $$$b$$$, and test whether this $$$x$$$ is valid or not.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long a, b;
cin >> a >> b;
long long res = 0, count = 1;
while (a || b) {
int b1 = a & 1, b2 = b & 1;
if (b1 == 1) {
if (b2 == 0) {
res += (1LL << count);
}
}
else if (b2 == 1) {
cout << -1 << endl; return;
}
a >>= 1; b >>= 1;
count++;
}
res >>= 1;
cout << res << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {
solve();
}
}
// Thanks Allah
source : 103150F — EZ Programming Contest #1 — Palindromicity The string $$$aaa...aaa$$$ with length $$$l$$$ has exactly $$${l+1}\choose{2}$$$ palindromic substrings. Therefore it suffices to find some triangular numbers that add up to exactly $$$n$$$.
To do this, we can greedily choose the largest triangular number less than $$$n$$$. It can be checked that the length of the string will be at most $$$300$$$ for the given constraints.
Alternatively, by Gauss's Eureka Theorem, we only need to express $$$n$$$ as the sum of three triangular numbers. We can just brute force the three triangular numbers and find the answer for each $$$n$$$. This runs in $$$O(n\sqrt{n})$$$ per test case if you lazily brute force, or $$$O(n)$$$ or $$$O(n\sqrt{n})$$$ per test case if you use some math to find the next smallest triangular number or use precomputation.
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
vector<ll>tri{0};
for(int i=1;i<=400;i++)tri.push_back(tri.back()+i);
int q;cin>>q;
while(q--){
ll s;cin>>s;
ll cs=0;string ss;
int cur=0;
while(cs<s){
int i=0;
while(tri[i+1]+cs<=s)i++;
cs+=tri[i];
for(int j=0;j<i;j++)ss+=char('a'+cur);
cur=(cur+1)%26;
}
cout<<ss<<"\n";
}
}
source : 102953-8 — CodeRams Algorithm Contest #2 — Number Placement For a given room of size $$$m$$$, it’s optimal to place the $$$m - 1$$$ smallest primes, and $$$1$$$, in the open cells. We can calculate the primes less than $$$10^7$$$ using the Sieve of Eratosthenes, and make a prefix sum array $$$P$$$, where we the $$$i_{th}$$$ element will pre calculate the sum of the first $$$i - 1$$$ primes and $$$1$$$. This way, $$$P[i]$$$ will be the minimum possible sum of numbers in a room of size $$$i$$$. Finally, we can run a DFS on the floor plan of the house to find the size of each room, and add $$$P[i]$$$ to the answer for each room size we find.
Proof of the first $$$m - 1$$$ prime numbers have minimum co-prime sum :
Suppose coprime numbers are not the first $$$m - 1$$$ primes and $$$1$$$, then there should at least be two numbers that have at least one common prime factor, which violates the condition that the numbers should be coprime.
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <fstream>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <list>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef vector<string> vs;
typedef pair<ll, ll> iil;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<ll> pqil;
typedef priority_queue<iil> pqiil;
typedef vector<iil> viil;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int, ii> iii;
typedef pair<iil, ll> iiil;
typedef vector<pair<pair<int,int>,int> > viii;
typedef vector<pair<pair<ll,ll>,ll> > viiil;
typedef vector<vl> vvl;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0 ; i < n ; i++)
#define rrep(i, m, n) for (int i = m ; i < n ; i++)
#define per(i, n) for (int i = n - 1 ; i >= 0 ; i--)
#define perr(i, m, n) for (int i = n - 1 ; i >= m ; i--)
#define INF 2000000000
ll MOD = 1000000007;
int mat[1005][1005];
bool vis[1005][1005];
int sm = 0;
int n;
bool comp[10000005];
vi p;
int MX = 10000005;
void dfs(int i, int j) {
if (i < 0 || i >= n || j < 0 || j >= n) return;
if (!mat[i][j]) return;
if (vis[i][j]) return;
vis[i][j] = true;
sm++;
dfs(i-1, j);
dfs(i+1, j);
dfs(i, j-1);
dfs(i, j+1);
}
void solve() {
for (int i = 2 ; i * i <= MX ; i++) {
if (comp[i]) continue;
for (int j = i * i ; j < MX ; j += i) {
comp[j] = true;
}
}
rep(i, MX) {
if (!comp[i] && i > 1) {
p.pb(i);
}
}
vl pf;
pf.pb(0);
pf.pb(1);
rep(i, p.size()) {
pf.pb(pf[pf.size()-1] + p[i]);
}
cin >> n;
rep(i, n) {
string s;
cin >> s;
rep(j, n) {
mat[i][j] = (s[j] == '.');
}
}
ll tot = 0;
rep(i, n) {
rep(j, n) {
if (!vis[i][j] && mat[i][j]) {
sm = 0;
dfs(i, j);
tot = (tot + pf[sm]);
}
}
}
//cout << p.size() << '\n';
cout << tot << '\n';
}
void querySolve() {
int t;
cin >> t;
for (int i = 0 ; i < t ; i++) {
solve();
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
//querySolve();
}
thank you for this problemset but can you please open the submission test i want to know what test fall my answer E404_Not_Found
Ok bro.
gonna wait to see doing an official round soon keep the work up
Thanks a lot <3
Unable to View the Contest. Can You Open it for Practise?
Hi, Thanks for mentioning, It's fixed now.
Still Showing : You are not allowed to view the contest
Did you click on this link https://codeforces.me/contestInvitation/b5139b6a02b1d5c6c0a310db27d0fc1321f09720 and it said you are not allowed?
Can you plz send and optimize the code of DIVSUM Problem?
https://codeforces.me/gym/415092/submission/185112616
Great problems.