Skip to content

Instantly share code, notes, and snippets.

View LostInKadath's full-sized avatar

LostInKadath LostInKadath

View GitHub Profile
'''
Task 83. Find the number of solutions of an equation
1/n = 1/x + 1/y
in natural numbers.
Make some manipulations:
1/n = 1/x + 1/y (1)
1/y = (x-n)/nx
y = nx/(x-n) (2)
y = n + n^2/(x-n) (3)
'''Task 84. Count a number of anagrams.
Number of letter permutations can be calculated as a factorial.
But if a word contains repeated letters, you should bear it in mind.
If some letter repeated twice, it gives us 2! cases.
If thrice - 3! cases.
'''
# Sorry for lib funcs. =)
from math import factorial
from functools import reduce
'''Task 85. An equation is given:
_ 1 _ 2 _ 3 _ ... _ n = k.
A symbol '_' can be replaced only by '+' or '-'.
For given integer k find minimum n, for which this equation has solutions.
The answer can be found in such way.
If k = 0, the answer is 3:
1 + 2 - 3 = 0.
If k < 0, the answer is the same for k > 0,
''' Task 86. A metal sheet is given, with its width and height.
Three equal square sheets are to cut from it.
Sides of squares are parallel to sides of the whole sheet.
Find the side of largest possible square.
Firstly we find the short and the long side.
Then we have two variants:
- to take two squares on the short side and two on the long side:
_ _ _ _ _
| | | |
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
#include <assert.h>
using namespace std;
long long comb(int n, int k)
{
if (n < 0 || k < 0)
return -1;
if (n == 0 || n < k)
@LostInKadath
LostInKadath / task88.cpp
Last active April 25, 2018 21:31
Binomial coefficient calculation with only __uint64 usage
#include <iostream>
#include <set>
using namespace std;
const unsigned __int64 MaxValue = 18446744073709551615;
unsigned __int64 cnk(unsigned __int64 n, unsigned __int64 k)
{
if (n < k)
return 0ull;
@LostInKadath
LostInKadath / task89.py
Last active April 29, 2018 17:17
task89.py
from functools import reduce
def C(n, k):
'''Combinations of k from n:
C(n, k) = n! / k! / (n-k)!
It is used when one should choose k elements from n without
repetitions, and the order of choise doesn't matter.
'''
if n < k:
return 0
@LostInKadath
LostInKadath / WaterVolume_2D.cpp
Last active May 2, 2018 16:23
Find a volume of water containing in a histogram
#include <iostream>
#include <vector>
using namespace std;
int WaterVolume_2D(const vector<int> &hills)
{
int water = 0;
int left(0), right(hills.size() - 1);
int highest_left(0), highest_right(0);
#include <iostream>
#include <vector>
using namespace std;
bool is_filled(int x, int y, const vector<int> &xs, const vector<int> &ys)
{
for (size_t i = 0; i < xs.size(); i++)
if (x == xs[i] && y == ys[i])
return true;
return false;