forked from harrypotter0/algorithms-in-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f9be25
commit 9d8ab08
Showing
17 changed files
with
1,480 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.