Skip to content

Commit dfe6e71

Browse files
committed
Pigeonhole
1 parent 1f613fd commit dfe6e71

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

DIVSUBS-d.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//codechef : pigeonhole
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
typedef long long ll;
5+
typedef vector<ll> vi;
6+
typedef vector<vi> vvi;
7+
const int mod = 1000000007;
8+
void printMat(vvi& a){for(int i=0;i<a.size();++i){for(int j=0;j<a[0].size();++j)cout<<a[i][j]<<" ";}cout<<"\n";}
9+
const int sze = 100010;
10+
11+
int main(){
12+
int t; cin >> t;
13+
// Each element of the multiset is a positive integer, not exceeding 109.
14+
//Alert sum can exceed int range
15+
16+
int arr[sze];
17+
ll sum[sze] = {};
18+
19+
while(t--){
20+
int n;
21+
cin >> n;
22+
23+
for(int i = 0; i < n; ++i){
24+
cin >> arr[i];
25+
sum[i+1] = arr[i] + sum[i]; //prev + cur
26+
}
27+
28+
int* remainder = new int[n];
29+
fill(remainder, remainder + n, -1);
30+
31+
int be = -1;
32+
int en = -1;
33+
34+
for(int i = 0; i <= n; ++i){
35+
int& curIdx = remainder[sum[i] % n];
36+
37+
if (curIdx != -1) {
38+
//this remainder has been previously visited
39+
be = curIdx;
40+
en = i;
41+
break;
42+
}
43+
else {
44+
curIdx = i;
45+
}
46+
}
47+
48+
cout << en-be <<"\n"; //no of elements in a range
49+
++be;
50+
while(be <= en) {
51+
cout << be << " ";
52+
be++;
53+
}
54+
55+
cout << "\n";
56+
delete [] remainder;
57+
58+
}
59+
}

SPP-d.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
typedef vector<ll> vi;
5+
typedef vector<vi> vvi;
6+
ll mod;// = 1000000007;
7+
void printMat(vvi& a) {
8+
for (int i = 0; i < a.size(); ++i)
9+
{ for (int j = 0; j < a[0].size(); ++j)cout << a[i][j] << " ";
10+
cout << "\n";
11+
}
12+
}
13+
14+
vvi mul(const vvi& a, const vvi& b) {
15+
vvi c(a.size(), vi(b[0].size(), 0));
16+
17+
for (int r = 0; r < a.size(); ++r) {
18+
for (int j = 0; j < b[0].size(); ++j) {
19+
for (int x = 0; x < b.size(); ++x) {
20+
c[r][j] += (a[r][x] % mod) * (b[x][j] % mod);
21+
c[r][j] %= mod;
22+
}
23+
}
24+
}
25+
return c;
26+
}
27+
28+
vvi pow(const vvi& a, int n) {
29+
if (n <= 1) return a;
30+
if (n & 1) return mul(a, pow(a, n - 1));
31+
32+
vvi ans = pow(a, n / 2);
33+
ans = mul(ans, ans);
34+
return ans;
35+
}
36+
37+
ll sum(int n, vi& b, vi& c) {
38+
if (n < 1) return 0;
39+
40+
int k = b.size();
41+
vvi t(k + 1, vi(k + 1, 0));
42+
43+
for (int i = 0; i <= k; ++i) {
44+
for (int j = 0; j <= k; ++j) {
45+
//for last row
46+
if (i != k && j == i + 1) t[i][j] = 1;
47+
else if (i == k && j != 0) t[i][j] = c[j-1];
48+
// else if (i == 0 && j == 1) t[i][j] = 1;
49+
}
50+
}
51+
t[0][0] = t[0][1] = 1;
52+
53+
vvi tn = pow(t, n);
54+
// printMat(tn);
55+
ll ans = 0;
56+
//the R0b0 is the answer
57+
for (int i = 0; i < k; ++i) {
58+
// cout << t[0][1+i] << endl;
59+
ans += (tn[0][i + 1] * b[i]) % mod;
60+
ans %= mod;
61+
}
62+
// cout << ans << endl;
63+
return ans;
64+
}
65+
66+
int main() {
67+
int t;
68+
cin >> t;
69+
while (t--) {
70+
int k; cin >> k;
71+
vi b(k);
72+
for (int i = 0; i < k; ++i) cin >> b[i];
73+
vi c(k);
74+
for (int i = 0; i < k; ++i) cin >> c[k - i - 1];
75+
ll m, n;
76+
cin >> m >> n >> mod;
77+
ll m_ = sum(m - 1, b, c);
78+
ll n_ = sum(n, b, c);
79+
// int ans = (n==1&& m<=1) ? b[0] : (n_ - m_ + mod);
80+
ll ans = (n_- m_ + mod) % mod;
81+
//ALWAYS handle negative mods
82+
cout << ans << "\n";
83+
}
84+
}

SUMSUMS-d_1.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
const ll N = 50000;
6+
ll n, t;
7+
ll cows[N];
8+
const ll m = 98765431;
9+
10+
//mult(ll[][] ll[][])
11+
void mul(ll a[][2], ll b[][2]){
12+
ll c[2][2]={};
13+
for(ll i = 0; i < 2; ++i){
14+
for(ll j = 0; j < 2; ++j){
15+
for(ll x = 0; x < 2; ++x){
16+
c[i][j] += (a[i][x] * b[x][j]) % m;
17+
c[i][j] %= m;
18+
}
19+
}
20+
}
21+
22+
for(ll i = 0; i < 2; ++i){
23+
for(ll j = 0; j < 2; ++j)
24+
a[i][j] = c[i][j];
25+
}
26+
}
27+
28+
29+
30+
//pow(ll[][], t)
31+
void pow(ll mat[][2], ll n){
32+
if (n <= 1) return;
33+
34+
if (n&1) {
35+
ll tmp[2][2] = {
36+
{mat[0][0], mat[0][1]},
37+
{mat[1][0], mat[1][1]}
38+
};
39+
pow(mat, n-1);
40+
mul(mat, tmp);
41+
return;
42+
}
43+
44+
pow(mat, n/2);
45+
mul(mat, mat);
46+
}
47+
48+
void transform(ll tn[][2]){
49+
if (t == 0) return;
50+
pow(tn, t);
51+
}
52+
53+
54+
int main(){
55+
cin >> n >> t;
56+
ll s =0;
57+
58+
for(ll i = 0; i < n; ++i){
59+
cin >> cows[i];
60+
s += cows[i] % m;
61+
s %= m;
62+
}
63+
ll tn[2][2] = {
64+
{n-1, 0},
65+
{1, -1}
66+
};
67+
68+
transform(tn);
69+
// cout << tn[0][0] << " " << tn[0][1] << " " << tn[1][0] << " " << tn[1][1] << " " << endl;;
70+
71+
for(ll i = 0; i < n; ++i){
72+
//S1 = (n - 1)S0
73+
//c[1] = S0 - c(0)
74+
75+
ll f[2][1] = {{s}, {cows[i]}};
76+
ll ans = (tn[1][0]*f[0][0]%m) + (tn[1][1] * f[1][0])%m;
77+
78+
//this might be negative
79+
cout << ((ans+m) % m) << "\n";
80+
81+
}
82+
}

SUMSUMS.cpp

Whitespace-only changes.

in

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
5
2+
2
3+
1 1
4+
1 1
5+
1 1 1000003
6+
2
7+
1 1
8+
1 1
9+
1 2 1000003
10+
2
11+
1 1
12+
1 1
13+
1 3 1000003
14+
2
15+
1 1
16+
1 1
17+
2 3 1000003
18+
2
19+
1 1
20+
1 1
21+
2 4 1000003

0 commit comments

Comments
 (0)