Mathematics: Birthday Paradox
Mathematics: Birthday Paradox
Birthday Paradox:
If there are two people in a room,
= 0.274%
0.9973 = 99.73%
people matching.
#include<iostream>
int minPeople(){
fferent birthdays
double res = 1;
int cnt = 0;
res *= prob;
cnt++;
cout<<cnt<<" "<<res<<endl;
for(int i = 1;i<cnt;i++){
//cout<<i<<endl;
return i;
return 0;
int main(){
cout<<minPeople()<<endl;
return 0;
Definition
First, let’s start with a definition. A linear recurrence relation is a
1) + f(i-2). Linear means that the previous terms in the definition are
only multiplied by a constant (possibly zero) and nothing else. So, this
Problem
Given f, a function defined as a linear recurrence relation. Compute
How to Solve
Break the problem in four steps. Fibonacci sequence will be used as an
example.
depends.
More precisely, K is the minimum integer such that f(i) doesn’t depend
on f(i-M), for all M > K. For Fibonacci sequence, because the relation
is:
then it must have the first K terms defined, otherwise the whole
f(2) = 1
second row is f(i+1), and so on, until K-th row is f(i+K-1). The initial
values of f are given in column vector F1 that has values f(1) through
f(K):
Suppose,
Putting i = k+1
f(k+1) = C1f(k) + C2f(k-1) + C3f(k-2) + ...... + Ckf(1)
C1 = 1, C2 = 1.
To obtain F3,
And so on. In general,
above, and then we can obtain f(N): it is exactly the first row of FN. In
A ^ p = A, if p = 1,
A ^ p = A * A ^ p-1, if p is odd
log N).
Variation
The recurrence relation may include a constant, i.e., the function is of
the form:
For example, let f(i) = 2f(i-1) + 3f(i-2) + 5. Then, the matrix equation
for this function is:
#include<iostream>
#include<vector>
ll k;
vector<ll> b;
vector<ll> a;
vector<ll> c;
for(int x = 1;x<=k;x++)
OD;
return C;
if(p == 1)
return A;
if(p & 1)
return mul(A,pow(A,p-1));
return mul(X,X);
ll fib(ll n){
//Base Case
if(n == 0)
return 0;
if(n <= k)
return b[n-1];
vector<ll> F1(k+1);
for(int i = 1;i<k+1;i++)
F1[i] = b[i-1];
for(int i = 1;i<=k;i++){
for(int j = 1;j<=k;j++){
if(i<k){
if(j == i+1)
T[i][j] = 1;
else T[i][j] = 0;
continue;
T = pow(T,n-1);
for(int i = 1;i<=k;i++){
int main(){
ll t,n,a;
scanf("%lld",&t);
while(t--){
scanf("%lld",&k);
for(int i = 0;i<k;i++){
scanf("%lld",&a);
b.push_back(a);
for(int i = 0;i<k;i++){
scanf("%lld",&a);
c.push_back(a);
scanf("%lld",&n);
printf("%lld\n",fib(n));
b.clear();
c.clear();
return 0;
}
Si = a1 + a2 + a3 + ......+ ai
with S0 = 0
Si = Si-1 + ai
Code
#include <iostream>
#include <vector>
ll m,n,MOD;
int k;
vector<ll> c;
vector<ll> b;
for(int i = 1;i<=k+1;i++){
for(int j = 1;j<=k+1;j++){
for(int x = 1;x<=k+1;x++)
OD;
return C;
if(p == 1)
return A;
if(p & 1)
matrix X = pow(A,p/2);
return mul(X,X);
ll fib(ll n){
//Base Case
if(!n)
return 0;
return b[n-1];
vector<ll> F1(k+2);
for(int i = 2;i<=k+1;i++){
F1[i] = (b[i-2]);
for(int i = 1;i<=k+1;i++){
for(int j = 1;j<=k+1;j++){
//Put 1 at (1,1)
T[i][j] = 1;
continue;
continue;
if(j == i+1){
T[i][j] = 1;
continue;
}
//else 0 in (i,j)
T[i][j] = 0;
T = pow(T,n);
ll res = 0;
for(int i = 1;i<=k+1;i++){
int main(){
int t;
ll a;
scanf("%d",&t);
while(t--){
scanf("%d",&k);
for(int i = 1;i<=k;i++){
scanf("%lld",&a);
b.push_back(a);
for(int i = 1;i<=k;i++){
scanf("%lld",&a);
c.push_back(a);
printf("%lld\n",tot);
c.clear();
b.clear();
return 0;
Pigeonhole Principle
The pigeonhole principle is a fairly simple idea to grasp.Say that you
have 7 pigeons and 6 pigeonholes. So, now you decide to start putting
| p | | p | | p | | p | | p | | p |
So, now, you have one pigeon left, and you can put it into any of the
pigeonholes.
|pp | | p | | p | | p | | p | | p |
The point is that when the number of pigeons > no. of pigeonholes,
strands, the average human head has about 150,000 hair strands. It is
safe to assume, then, that no human head has more than 1,000,000
Hint:
a % N = x
b % N = x
Then, (b-a) % N = (b % N - a % N) = (x - x) = 0
b0 = 0
b1 = a1
b2 = a1 + a2
.
.
bN = a1 + a2 + a3 + a4 +.......+ aN
Therefore, if there are two values with equal residues modulo N among
b0, b1, …, bN then we take the first one for L-1 and the second one for
always exist.
Code
#include<bits/stdc++.h>
#define N (1e6+10)
vector<int> v;
vector<int> pre;
int main(){
int t,n,x,l = 0,r = 0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 0;i<n;i++){
scanf("%d",&x);
v.push_back(x % n);
pre.push_back(0);
for(int i = 0;i<n;i++)
for(int i = 0;i<n+1;i++){
ime
if(pos[pre[i]] == -1){
pos[pre[i]] = i;
tered
else{
l = pos[pre[i]] + 1;
r = i;
break;
//is (R-L+1)
printf("%d\n",r - l + 1);
for(int i = l;i<=r;i++)
printf("%d ",i);printf("\n");
v.clear();
pre.clear();
return 0;
T = O(N)
exactly ‘1’ bit. We have to find out 4 integers such that their XOR is
equal to 0.
{X129, X130}. But there exists only 64 possible position for the set bit
positions say {Xi, Xi+1} and {Xj, Xj+1}. If we take xor of pair of these
Thus, by pigeonhole principle for all n >= 130, we will always find 4
integers such that their XOR is 0. For n < 130, we can iterate for 3
values of A[i], A[j], A[k] and do a binary search to find 4th number
Code
#include<iostream>
#include<vector>
#include<stdio.h>
int main(){
scanf("%llu",&n);
for(int i = 0;i<n;i++){
scanf("%llu",&a);
v.push_back(a);
printf("Yes");
return 0;
for(int i = 0;i<n;i++){
for(int j = i+1;j<n;j++){
for(int l = k+1;l<n;l++){
printf("Yes\n");
return 0;
printf("No");
return 0;
Ques. Holi
Given a weighted tree, consider there are N people in N nodes. You
and no node contains more than one person under the constraint that
http://www.spoj.com/problems/HOLI/
subtrees, if one side has n nodes, the other side will have N - n nodes.
Also, note that, min(n, N-n) people will be crossing the edge from each
principle in one side, we will get more people than available node
#include<iostream>
#include<vector>
#include<string.h>
vector<int> adj[NN];
vector<int> w[NN];
int cnt[100000+10];
fs
bool vis[100000+10];
int n;
vis[v] = 1;
int u = adj[v][i];
if(vis[u])
continue;
dfs(u);
cnt[v] += cnt[u];
return;
vis[v] = 1;
for(int i = 0;i<adj[v].size();i++){
int u = adj[v][i];
if(vis[u])
continue;
hifted
[v][i]
ol(u);
return res;
int main(){
int t,a,x,y,in = 1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 0;i<=n;i++){
adj[i].clear();
w[i].clear();
cnt[i] = 1;
vis[i] = 0;
for(int i = 0;i<n-1;i++){
scanf("%d %d %d",&x,&y,&a);
--x;
--y;
adj[x].push_back(y);
adj[y].push_back(x);
w[x].push_back(a);
w[y].push_back(a);
dfs(0);
memset(vis,0,sizeof vis);
++in;
return 0;
cardinality of A.
If A and B are not disjoint, we get the simplest form of the Inclusion-
Exclusion Principle:
In the more general case where there are n different sets Ai, the
formula for the Inclusion-Exclusion Principle becomes:
i.e. composite but not divisible by 2,3 or 5 (Ex- 49, 77, 91). Given
that there are 168 primes upto 1000.
∩ 5| + |2 ∩ 3 ∩ 5|
= 733
So, there exists 733 integers upto 1000 which have atleast 2,3 or 5 as
Since there are 168 prime numbers upto 1000, but we have already
such that A[i] >=1 and A[i] <= M and LCM(A[1], A[2], …, A[N]) is
divisible by D.
We have to find the sum of the answers with D = L, L+1, …,R modulo
10^9 + 7.
Note: The maximum value of the array elements can be 1000, the
maximum number of distinct prime factors possible is 4 (2 * 3 * 5 * 7 *
11 > 1000).
p^a: P
q^b: Q
r^c: R
s^d: S
To calculate y:
So, calculate the number of arrays such that either (P or its multiple
present).
) + power(m - m/S,n);
/R + m/(Q*R), n).....
- m/(P*Q*R),n)......
y = A - B + C - D
ans = m ^ n - y
= m ^ n - A + B - C + D
Code
#include<iostream>
#include<stdio.h>
#include<algorithm>
int prime[1010],F[1010][5],cnt = 0;
if(!a)
return 0;
if(b == 1)
return a;
ll res = 1;
while(b){
if(b & 1)
res *= (a % MOD);
res %= MOD;
a *= a;
a %= MOD;
b >>= 1;
}
return res % MOD;
int main(){
//Prime sieve
bool a[1010];
for(int i = 0;i<=1000;i++)
a[i] = 1;
if(a[i]){
for(int j = 2*i;j<=1000;j+=i)
a[j] = 0;
// in increasing order.
for(int i = 2;i<=1000;i++){
if(a[i]){
prime[cnt++] = i;
00
for(int i = 0;i<=1000;i++){
for(int j = 0;j<5;j++){
F[i][j] = 1;
//upto 1000
//value of p in factorisation of i
//Ex: 18 = 2*3*3
//F[18][0] = 2
//F[18][1] = 9
for(int i = 1;i<=1000;i++){
for(int f = 0;f<cnt;f++){
int p = 1;
p *= prime[f];
num /= prime[f];
if(p > 1)
F[i][in++] = p;
int t,L,R,m,n;
scanf("%d",&t);
while(t--){
scanf("%d %d %d %d",&n,&m,&L,&R);
int p,q,r,s;
for(int i = L;i<=R;i++){
p = F[i][0];
q = F[i][1];
r = F[i][2];
s = F[i][3];
dl;
A %= MOD;
*s), n);
B %= MOD;
p*s)+m/(q*s)-m/(p*q*s),n) + powmod(m-m/s-m/q-m/r+m/(s*q)+
m/(s*r)+m/(q*r)-m/(s*q*r),n) + powmod(m-m/p-m/r-m/s+m/(
p*r)+m/(p*s)+m/(r*s)-m/(p*r*s),n);
C %= MOD;
D = powmod(m-m/p-m/q-m/r-m/s+m/(p*q)+m/(q*r)+
m/(p*r)+m/(r*s)+m/(p*s)+m/(q*s) - m/(p*q*r)-m/(q*r*s) - m
D %= MOD;
//cout<<i<<" "<<(powmod(m,n) - A + B - C + D
+ 2*MOD) % MOD<<endl;
% MOD;
res %= MOD;
printf("%lld\n",res);
return 0;
Mathematical Expectation
Mathematically, for a discrete variable X with probability function P(X),
the expected value E(X) is given by Σ xiP(xi) the summation runs over
all the distinct values xi that the variable can take.
is { 1,2,3,4,5,6} and each of this outcome has the same probability 1/6.
random variable.
probability.
Thus, observe that in the problems where there are two events, where
desirable event is p, then the expected number of trials done to get the
one event is desirable and all others are undesirable, and the
http://www.spoj.com/submit/FAVDICE/id=18369020
cereal boxes must the coupon collector buy so that the coupon
coupon collector to collect the i-th new coupon after the i−1th
coupon has already been collected. (Note: this does NOT mean
assign numbers to coupons and then collect the i-th coupon. Instead,
this means that after Xi boxes, the coupon collector would have
collected i distinct coupons, but with only Xi−1 boxes, the coupon
After the i−1-th coupon has been collected, then there are n−(i−1)
possible coupons that could be the new i-th coupon. Each trial of
p = (n-(i-1)) / n
bernaulli trial, the expected number of trials for i-th success is 1/p i.e.
Code
#include<iostream>
using namespace std;
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 1;i<=n;i++){
ans += n/(i*1.0);
printf("%.9lf\n",ans);
return 0;