Skip to content

Commit 979b4b8

Browse files
committed
Codemania
1 parent 3b90ba5 commit 979b4b8

File tree

6 files changed

+242
-61
lines changed

6 files changed

+242
-61
lines changed

CP/Codeforces Round #464 (Div. 2)/prob1.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
1-
n = int(input())
2-
arr = map(int,raw_input().split())
3-
a=[]
4-
f=0
5-
j=0
6-
for i in range(0,n):
7-
vec = arr[i]
8-
while j<n and vec!=n:
9-
if(arr[i] in a):
10-
# print(a)
11-
f=1
12-
break
13-
if(vec!=arr[vec-1]):
14-
vec=arr[vec-1]
15-
a.append(vec)
16-
j+=1
17-
print(a)
18-
if(f and len(a)==3): break
19-
del a[:]
20-
21-
# del lst[:]
22-
23-
# print(a)
24-
if(f and len(a)==3):
25-
print("YES")
26-
else:
27-
print("NO")
28-
1+
def readInts():
2+
return map(int, input().split())
3+
def readInt():
4+
return int(input())
5+
def readIntsindex0():
6+
return list(map(lambda x: int(x) - 1, input().split()))
7+
8+
n = readInt()
9+
a = readIntsindex0()
10+
ans = False
11+
12+
for i in range(n):
13+
if a[a[a[i]]] == i:
14+
ans = True
15+
break
16+
17+
print('YES' if ans else 'NO')
2918

3019
'''
3120
5
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def readInts():
2+
return list(map(int, input().split()))
3+
def readInt():
4+
return int(input())
5+
def readIntsindex0():
6+
return list(map(lambda x: int(x) - 1, input().split()))
7+
8+
9+
n, k = readInts()
10+
a = readInts()
11+
12+
best = -1
13+
sel = -1
14+
for i in range(k):
15+
cur = n - n % a[i]
16+
if best < cur:
17+
best = cur
18+
sel = i
19+
20+
print('%d %d' % (sel + 1, n // a[sel]))

CP/Codemania/prob4.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
const int mod=1e9+7;
5+
int po(int x,int k)
6+
{
7+
int sum=1;
8+
while(k)
9+
{
10+
if(k&1)
11+
{
12+
sum=1LL*sum*x%mod;
13+
}
14+
x=1LL*x*x%mod;
15+
k>>=1;
16+
}
17+
return sum;
18+
}
19+
int main()
20+
{
21+
int t;
22+
scanf("%d",&t);
23+
while(t--)
24+
{
25+
int n,m;
26+
scanf("%d%d",&n,&m);
27+
printf("%d\n",1LL*po(n+1,m-1)*(n-m+1)%mod*po (2,m)%mod);
28+
}
29+
return 0;
30+
}

CP/Codemania/prob4.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import math
22
import sys
33
n=int(input())
4-
mod = pow(10,9)+7
4+
mod = 10000000007
5+
ans =1
56
for i in range(n):
6-
p , m = map(int , raw_input().split())
7-
ans1 = pow(2*(p+1) , m , mod)%mod
8-
ans2 = pow((p+1) , mod-2 , mod)%mod
9-
ans3 = (p+1-m)%mod
7+
n,m = map(int,raw_input().split())
8+
ans1 = pow(2,m)%mod
9+
ans2 = pow(n+1,m-1)%mod
10+
ans3 = (n+1-m)%mod
1011
ans = (ans1*ans2*ans3)%mod
11-
# print(ans1,ans2,ans3)
1212
print(ans)

CP/Codemania/prob5.cpp

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,72 @@
11
#include "bits/stdc++.h"
22
using namespace std;
33
typedef long long ll;
4-
const ll siz = 5e5 + 4;
5-
const ll inf = 1e18;
6-
ll arr[siz], tree[4 * siz]; //Size of Segment tree is never going to be more than 4 * siz.
7-
8-
void build(ll node, ll start, ll end) {
9-
if(start == end)
10-
tree[node] = arr[start]; // Leaf node will have a single element.
11-
else {
12-
ll mid = (start + end) / 2;
13-
build(2 * node, start, mid); // Recurse on the left child.
14-
build(2 * node + 1, mid + 1, end); // Recurse on the right child.
15-
// Internal node will have the sum of both of its children.
16-
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
17-
}
4+
5+
#define N 55555 + 4
6+
#define inf 0x7fffffff
7+
/**
8+
* In this code we have a very large array called arr, and very large set of operations
9+
* Operation #1: Increment the elements within range [i, j] with value val
10+
* Operation #2: Get max element within range [i, j]
11+
* Build tree: build(1, 0, N-1)
12+
* Update tree: update(1, 0, N-1, i, j, value)
13+
* Query tree: query(1, 0, N-1, i, j)
14+
*/
15+
ll arr[N];
16+
ll tree[4*N];
17+
18+
/**
19+
* Build and init tree
20+
*/
21+
void build(ll node, ll a, ll b) {
22+
if(a > b) return; // Out of range
23+
24+
if(a == b) { // Leaf node
25+
tree[node] = arr[a]; // Init value
26+
return;
27+
}
28+
29+
build(node*2, a, (a+b)/2); // Init left child
30+
build(node*2+1, 1+(a+b)/2, b); // Init right child
31+
32+
tree[node] = max(tree[node*2], tree[node*2+1]); // Init root value
33+
}
34+
35+
/**
36+
* Increment elements within range [i, j] with value value
37+
*/
38+
void update(ll node, ll a, ll b, ll i, ll j, ll value) {
39+
40+
if(a > b || a > j || b < i) // Current segment is not within range [i, j]
41+
return;
42+
43+
if(a == b) { // Leaf node
44+
tree[node] += value;
45+
return;
46+
}
47+
48+
update(node*2, a, (a+b)/2, i, j, value); // Updating left child
49+
update(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child
50+
51+
tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
1852
}
1953

20-
ll query(ll node, ll start, ll end, ll l, ll r) {
21-
if(r < start || end < l)
22-
return -inf; // range represented by a node is completely outside the given range.
23-
if(l <= start && end <= r)
24-
return tree[node]; // range represented by a node is completely inside the given range.
25-
// range represented by a node is partially inside and partially outside the given range.
26-
ll mid = (start + end) / 2;
27-
ll s1 = query(2 * node, start, mid, l, r);
28-
ll s2 = query(2 * node + 1, mid + 1, end, l, r);
29-
return max(s1 ,s2);
54+
/**
55+
* Query tree to get max element value within range [i, j]
56+
*/
57+
ll query(ll node, ll a, ll b, ll i, ll j) {
58+
59+
if(a > b || a > j || b < i) return -inf; // Out of range
60+
61+
if(a >= i && b <= j) // Current segment is totally within range [i, j]
62+
return tree[node];
63+
64+
ll q1 = query(node*2, a, (a+b)/2, i, j); // Query left child
65+
ll q2 = query(1+node*2, 1+(a+b)/2, b, i, j); // Query right child
66+
67+
ll res = max(q1, q2); // Return final result
68+
69+
return res;
3070
}
3171

3272
int main(){
@@ -47,22 +87,34 @@ int main(){
4787
}
4888
sort(v.begin(), v.end());
4989
sort(v1.begin(), v1.end());
90+
5091
arr[0] = v[0].second;
5192
for(ll i = 1; i < v1.size(); ++i){
5293
arr[i] = arr[i - 1] + v[i].second;
5394
}
54-
/*for(int i = 0; i < v1.size(); ++i){
55-
cout<<v[i].first<<" "<<arr[i]<<"\n";
56-
}*/
95+
96+
// build(1, 0, N-1);
97+
98+
// update(1, 0, N-1, 0, 6, 5); // Increment range [0, 6] by 5
99+
// update(1, 0, N-1, 7, 10, 12); // Incremenet range [7, 10] by 12
100+
// update(1, 0, N-1, 10, N-1, 100); // Increment range [10, N-1] by 100
101+
102+
// cout << query(1, 0, N-1, 0, N-1) << endl; // Get max element in range [0, N-1]
103+
57104
build(1, 0, v1.size() - 1);
58105
while(q--){
59106
ll l, r;
60107
scanf("%lld %lld", &l, &r);
61108
ll left = lower_bound(v1.begin(), v1.end(), l) - v1.begin();
109+
// cout<<left<<" "<<v1[left]<<" "<<l<<endl;
62110
if(v1[left] > l && left > 0)
63111
left--;
64112
ll right = upper_bound(v1.begin(), v1.end(), r) - v1.begin();
65113
right--;
114+
// cout<<left<<" "<<v1[left]<<" "<<l<<endl;
115+
116+
// cout<<right<<" "<<v1[right]<<" "<<r<<endl;
117+
66118
if(right < 0 || v1[v1.size() - 1] < l){
67119
//cout<<"OK\n";
68120
printf("0\n");

CP/Codemania/prob6.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
const int MAX=400005;
4+
int timer,counter,n,m,disk[MAX],low[MAX],vis[MAX],scc[MAX];
5+
stack<int> st;
6+
vector<int> adj[MAX];
7+
int neg(int a){ return (a>n ? a-n : a+n); }
8+
void dfs_tarjan(int node){
9+
disk[node]=low[node]=++timer;
10+
vis[node]=1;
11+
st.push(node);
12+
for(int child : adj[node]){
13+
if(vis[child]==1){
14+
low[node]=min(low[node],disk[child]);
15+
}else if(vis[child]==0){
16+
dfs_tarjan(child);
17+
low[node]=min(low[node],low[child]);
18+
}
19+
}
20+
if(disk[node]==low[node]){
21+
counter++;
22+
while(st.top()!=node){
23+
scc[st.top()]=counter;
24+
vis[st.top()]=-1;
25+
st.pop();
26+
}
27+
scc[st.top()]=counter;
28+
vis[st.top()]=-1;
29+
st.pop();
30+
}
31+
}
32+
int main(){
33+
int test,a,b,i;
34+
scanf("%d",&test);
35+
while(test--){
36+
counter=timer=0;
37+
scanf("%d%d",&n,&m);
38+
for(i=1;i<=2*n;i++){
39+
vis[i]=disk[i]=low[i]=scc[i]=0;
40+
adj[i].clear();
41+
}
42+
for(i=1;i<=n;i++){
43+
adj[neg(n+i)].push_back(i);
44+
adj[neg(i)].push_back(n+i);
45+
adj[n+i].push_back(neg(i));
46+
adj[i].push_back(neg(n+i));
47+
}
48+
for(i=1;i<=m;i++){
49+
scanf("%d%d",&a,&b);
50+
adj[neg(a)].push_back(b);
51+
adj[neg(b)].push_back(a);
52+
}
53+
for(int node=1;node<=2*n;node++){
54+
if(vis[node]==0)
55+
dfs_tarjan(node);
56+
}
57+
for(i=1;i<=n;i++){
58+
if(scc[i]==scc[neg(i)])
59+
break;
60+
}
61+
if(i<=n){
62+
printf("boring\n");
63+
}else{
64+
printf("interesting\n");
65+
}
66+
}
67+
return 0;
68+
}
69+
/*
70+
71+
Input:
72+
2
73+
2 6
74+
3 1
75+
1 4
76+
2 4
77+
2 3
78+
4 3
79+
2 1
80+
4 4
81+
1 3
82+
2 4
83+
5 7
84+
6 8
85+
86+
Output:
87+
boring
88+
interesting
89+
90+
*/

0 commit comments

Comments
 (0)