Skip to content

Commit

Permalink
Codenigma
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Mar 16, 2018
1 parent 9f9be25 commit 9d8ab08
Show file tree
Hide file tree
Showing 17 changed files with 1,480 additions and 40 deletions.
68 changes: 68 additions & 0 deletions CP/ Codenigma/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Akash Kandpal
# My Domain => http://harrypotter.tech/
# from fractions import gcd
import math
# from itertools import permutations
# import statistics

def readInts():
return list(map(int, raw_input().strip().split()))
def readInt():
return int(raw_input())
def readIntsindex0():
return list(map(lambda x: int(x) - 1, input().split()))
def readStrs():
return raw_input().split()
def readStr():
return raw_input()
def numlistTostr(list1):
return ''.join(list1)
def strlistTostr(list1):
return ''.join(str(e) for e in list1)
def strTolist(str):
return str.split()
def strlistTointlist(str):
return map(int, str)
def slicenum(number,x):
return int(str(number)[:x])
def precise(num):
return "{0:.10f}".format(num)
def rsorted(a):
return sorted(a,reverse=True)
def binar(x):
return '{0:031b}'.format(x)
def findpermute(word):
perms = [''.join(p) for p in permutations(word)]
return set(perms)
def findsubsets(S,m):
return set(itertools.combinations(S, m))
def sort1(yy,index):
return yy.sort(key = lambda x:x[index])
def reversepair(yy):
return yy[::-1]

MOD = 10 ** 9 + 7

for __ in range(readInt()):
n = readInt()
# maxi = -1e9
# for i in range(n+1):
# for j in range(n+1):
# # print i,j
# if(abs(i**2-j**2)==n):
# print i,j
# maxi = max(maxi,i+j)
# print maxi
print n

'''
Input:
3
1
7
35
Output:
1
'''
60 changes: 60 additions & 0 deletions CP/ Codenigma/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Akash Kandpal
# My Domain => http://harrypotter.tech/
# from fractions import gcd
import math
# from itertools import permutations
# import statistics

def readInts():
return list(map(int, raw_input().strip().split()))
def readInt():
return int(raw_input())
def readIntsindex0():
return list(map(lambda x: int(x) - 1, input().split()))
def readStrs():
return raw_input().split()
def readStr():
return raw_input()
def numlistTostr(list1):
return ''.join(list1)
def strlistTostr(list1):
return ''.join(str(e) for e in list1)
def strTolist(str):
return str.split()
def strlistTointlist(str):
return map(int, str)
def slicenum(number,x):
return int(str(number)[:x])
def precise(num):
return "{0:.10f}".format(num)
def rsorted(a):
return sorted(a,reverse=True)
def binar(x):
return '{0:031b}'.format(x)
def findpermute(word):
perms = [''.join(p) for p in permutations(word)]
return set(perms)
def findsubsets(S,m):
return set(itertools.combinations(S, m))
def sort1(yy,index):
return yy.sort(key = lambda x:x[index])
def reversepair(yy):
return yy[::-1]

MOD = 10 ** 9 + 7

for __ in range(readInt()):
n,m = readInts()
ans = m*(2*m+1)*(2*m-1)/3
# print ans
ans -= n*(2*n+1)*(2*n-1)/3
print ans % MOD

'''
Input:
1
1 4
Output:
83
'''
114 changes: 114 additions & 0 deletions CP/ Codenigma/p3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// C++ program to to find all pairs such that
// a % b = k.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

// Utiltity function to find the divisors of
// n and store in vector v[]
vector<int> findDivisors(int n)
{
vector<int> v;

// Vector is used to store the divisors
for (int i = 1; i<= sqrt(n); i++)
{
if (n%i == 0)
{
// If n is a square number, push
// only one occurrence
if (n/i == i)
v.push_back(i);
else
{
v.push_back(i);
v.push_back(n/i);
}
}
}
return v;
}

// Function to find pairs such that (a%b = k)
ll printPairs(ll arr[], ll n, ll k)
{
// Store all the elements in the map
// to use map as hash for finding elements
// in O(1) time.
int ans =0;
unordered_map<int, bool> occ;
for (int i = 0; i < n; i++)
occ[arr[i]] = true;

// bool isPairFound = false;
for (int i = 0; i<n ; i++ )
{
// Print all the pairs with (a, b) as
// (k, numbers greater than k) as
// k % (num (> k)) = k i.e. 2%4 = 2
if (occ[k] && k < arr[i])
{
// cout << "(" << k << ", " << arr[i] << ") ";
ans +=1;
// isPairFound = true;
}

// Now check for the current element as 'a'
// how many b exists such that a%b = k
if (arr[i] >= k)
{
// find all the divisors of (arr[i]-k)
vector<int> v = findDivisors(arr[i]-k);

// Check for each divisor i.e. arr[i] % b = k
// or not, if yes then print that pair.
for (int j = 0; j < v.size(); j++)
{
if (arr[i]%v[j] == k &&
arr[i]!= v[j] &&
occ[v[j]])
{
ans+=1;
// cout << "(" << arr[i] << ", "
// << v[j] << ") ";
// isPairFound = true;
}
}

// Clear vector
v.clear();
}
}

return ans;
}

// Driver program
int main()
{
// your code goes here
#ifdef JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ll arr[651010];
ll n,k,t;
cin>>t;
while(t--){
cin>>n>>k;
for(ll i =0 ;i<n;i++){
arr[i]=i+1;
}
ll ans = printPairs(arr, n, k);
if (ans == 0)
cout <<"0"<<endl;
else
cout<<ans<<endl;
}

return 0;
}
2 changes: 1 addition & 1 deletion CP/ February Challenge 2018/prob3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main() {
#ifdef JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
#endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
Expand Down
Loading

0 comments on commit 9d8ab08

Please sign in to comment.