Skip to content

Commit

Permalink
Codemania
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Feb 18, 2018
1 parent 3b90ba5 commit 979b4b8
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 61 deletions.
45 changes: 17 additions & 28 deletions CP/Codeforces Round #464 (Div. 2)/prob1.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
n = int(input())
arr = map(int,raw_input().split())
a=[]
f=0
j=0
for i in range(0,n):
vec = arr[i]
while j<n and vec!=n:
if(arr[i] in a):
# print(a)
f=1
break
if(vec!=arr[vec-1]):
vec=arr[vec-1]
a.append(vec)
j+=1
print(a)
if(f and len(a)==3): break
del a[:]

# del lst[:]

# print(a)
if(f and len(a)==3):
print("YES")
else:
print("NO")

def readInts():
return map(int, input().split())
def readInt():
return int(input())
def readIntsindex0():
return list(map(lambda x: int(x) - 1, input().split()))

n = readInt()
a = readIntsindex0()
ans = False

for i in range(n):
if a[a[a[i]]] == i:
ans = True
break

print('YES' if ans else 'NO')

'''
5
Expand Down
20 changes: 20 additions & 0 deletions CP/Codeforces Round #464 (Div. 2)/prob2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def readInts():
return list(map(int, input().split()))
def readInt():
return int(input())
def readIntsindex0():
return list(map(lambda x: int(x) - 1, input().split()))


n, k = readInts()
a = readInts()

best = -1
sel = -1
for i in range(k):
cur = n - n % a[i]
if best < cur:
best = cur
sel = i

print('%d %d' % (sel + 1, n // a[sel]))
30 changes: 30 additions & 0 deletions CP/Codemania/prob4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;

const int mod=1e9+7;
int po(int x,int k)
{
int sum=1;
while(k)
{
if(k&1)
{
sum=1LL*sum*x%mod;
}
x=1LL*x*x%mod;
k>>=1;
}
return sum;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
printf("%d\n",1LL*po(n+1,m-1)*(n-m+1)%mod*po (2,m)%mod);
}
return 0;
}
12 changes: 6 additions & 6 deletions CP/Codemania/prob4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import math
import sys
n=int(input())
mod = pow(10,9)+7
mod = 10000000007
ans =1
for i in range(n):
p , m = map(int , raw_input().split())
ans1 = pow(2*(p+1) , m , mod)%mod
ans2 = pow((p+1) , mod-2 , mod)%mod
ans3 = (p+1-m)%mod
n,m = map(int,raw_input().split())
ans1 = pow(2,m)%mod
ans2 = pow(n+1,m-1)%mod
ans3 = (n+1-m)%mod
ans = (ans1*ans2*ans3)%mod
# print(ans1,ans2,ans3)
print(ans)
106 changes: 79 additions & 27 deletions CP/Codemania/prob5.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll siz = 5e5 + 4;
const ll inf = 1e18;
ll arr[siz], tree[4 * siz]; //Size of Segment tree is never going to be more than 4 * siz.

void build(ll node, ll start, ll end) {
if(start == end)
tree[node] = arr[start]; // Leaf node will have a single element.
else {
ll mid = (start + end) / 2;
build(2 * node, start, mid); // Recurse on the left child.
build(2 * node + 1, mid + 1, end); // Recurse on the right child.
// Internal node will have the sum of both of its children.
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

#define N 55555 + 4
#define inf 0x7fffffff
/**
* In this code we have a very large array called arr, and very large set of operations
* Operation #1: Increment the elements within range [i, j] with value val
* Operation #2: Get max element within range [i, j]
* Build tree: build(1, 0, N-1)
* Update tree: update(1, 0, N-1, i, j, value)
* Query tree: query(1, 0, N-1, i, j)
*/
ll arr[N];
ll tree[4*N];

/**
* Build and init tree
*/
void build(ll node, ll a, ll b) {
if(a > b) return; // Out of range

if(a == b) { // Leaf node
tree[node] = arr[a]; // Init value
return;
}

build(node*2, a, (a+b)/2); // Init left child
build(node*2+1, 1+(a+b)/2, b); // Init right child

tree[node] = max(tree[node*2], tree[node*2+1]); // Init root value
}

/**
* Increment elements within range [i, j] with value value
*/
void update(ll node, ll a, ll b, ll i, ll j, ll value) {

if(a > b || a > j || b < i) // Current segment is not within range [i, j]
return;

if(a == b) { // Leaf node
tree[node] += value;
return;
}

update(node*2, a, (a+b)/2, i, j, value); // Updating left child
update(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child

tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
}

ll query(ll node, ll start, ll end, ll l, ll r) {
if(r < start || end < l)
return -inf; // range represented by a node is completely outside the given range.
if(l <= start && end <= r)
return tree[node]; // range represented by a node is completely inside the given range.
// range represented by a node is partially inside and partially outside the given range.
ll mid = (start + end) / 2;
ll s1 = query(2 * node, start, mid, l, r);
ll s2 = query(2 * node + 1, mid + 1, end, l, r);
return max(s1 ,s2);
/**
* Query tree to get max element value within range [i, j]
*/
ll query(ll node, ll a, ll b, ll i, ll j) {

if(a > b || a > j || b < i) return -inf; // Out of range

if(a >= i && b <= j) // Current segment is totally within range [i, j]
return tree[node];

ll q1 = query(node*2, a, (a+b)/2, i, j); // Query left child
ll q2 = query(1+node*2, 1+(a+b)/2, b, i, j); // Query right child

ll res = max(q1, q2); // Return final result

return res;
}

int main(){
Expand All @@ -47,22 +87,34 @@ int main(){
}
sort(v.begin(), v.end());
sort(v1.begin(), v1.end());

arr[0] = v[0].second;
for(ll i = 1; i < v1.size(); ++i){
arr[i] = arr[i - 1] + v[i].second;
}
/*for(int i = 0; i < v1.size(); ++i){
cout<<v[i].first<<" "<<arr[i]<<"\n";
}*/

// build(1, 0, N-1);

// update(1, 0, N-1, 0, 6, 5); // Increment range [0, 6] by 5
// update(1, 0, N-1, 7, 10, 12); // Incremenet range [7, 10] by 12
// update(1, 0, N-1, 10, N-1, 100); // Increment range [10, N-1] by 100

// cout << query(1, 0, N-1, 0, N-1) << endl; // Get max element in range [0, N-1]

build(1, 0, v1.size() - 1);
while(q--){
ll l, r;
scanf("%lld %lld", &l, &r);
ll left = lower_bound(v1.begin(), v1.end(), l) - v1.begin();
// cout<<left<<" "<<v1[left]<<" "<<l<<endl;
if(v1[left] > l && left > 0)
left--;
ll right = upper_bound(v1.begin(), v1.end(), r) - v1.begin();
right--;
// cout<<left<<" "<<v1[left]<<" "<<l<<endl;

// cout<<right<<" "<<v1[right]<<" "<<r<<endl;

if(right < 0 || v1[v1.size() - 1] < l){
//cout<<"OK\n";
printf("0\n");
Expand Down
90 changes: 90 additions & 0 deletions CP/Codemania/prob6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<bits/stdc++.h>
using namespace std;
const int MAX=400005;
int timer,counter,n,m,disk[MAX],low[MAX],vis[MAX],scc[MAX];
stack<int> st;
vector<int> adj[MAX];
int neg(int a){ return (a>n ? a-n : a+n); }
void dfs_tarjan(int node){
disk[node]=low[node]=++timer;
vis[node]=1;
st.push(node);
for(int child : adj[node]){
if(vis[child]==1){
low[node]=min(low[node],disk[child]);
}else if(vis[child]==0){
dfs_tarjan(child);
low[node]=min(low[node],low[child]);
}
}
if(disk[node]==low[node]){
counter++;
while(st.top()!=node){
scc[st.top()]=counter;
vis[st.top()]=-1;
st.pop();
}
scc[st.top()]=counter;
vis[st.top()]=-1;
st.pop();
}
}
int main(){
int test,a,b,i;
scanf("%d",&test);
while(test--){
counter=timer=0;
scanf("%d%d",&n,&m);
for(i=1;i<=2*n;i++){
vis[i]=disk[i]=low[i]=scc[i]=0;
adj[i].clear();
}
for(i=1;i<=n;i++){
adj[neg(n+i)].push_back(i);
adj[neg(i)].push_back(n+i);
adj[n+i].push_back(neg(i));
adj[i].push_back(neg(n+i));
}
for(i=1;i<=m;i++){
scanf("%d%d",&a,&b);
adj[neg(a)].push_back(b);
adj[neg(b)].push_back(a);
}
for(int node=1;node<=2*n;node++){
if(vis[node]==0)
dfs_tarjan(node);
}
for(i=1;i<=n;i++){
if(scc[i]==scc[neg(i)])
break;
}
if(i<=n){
printf("boring\n");
}else{
printf("interesting\n");
}
}
return 0;
}
/*
Input:
2
2 6
3 1
1 4
2 4
2 3
4 3
2 1
4 4
1 3
2 4
5 7
6 8
Output:
boring
interesting
*/

0 comments on commit 979b4b8

Please sign in to comment.