Skip to content

Commit 3136237

Browse files
committed
costest
1 parent 84f75d1 commit 3136237

10 files changed

+458
-82
lines changed

Gen2_0_PP/Contest/closesprimeNo2.py

-25
This file was deleted.

Gen2_0_PP/Contest/collectTheCoins.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'''
2+
There is a straight line with N points. A coin is located at each point starting from 1 and ending at N.
3+
4+
At N+1 th position, an additional M coins are kept.
5+
6+
A person starts walking with a bag from position 0 towards position N+1. At each position, i where 1 <= i <= N :
7+
8+
He collects the coin present at the position i.
9+
10+
If i divides both N and M, he empties his bag by throwing away all the coins he had collected till now.
11+
12+
Finally he collects the M coins present at position N+1.
13+
14+
Can you tell how many coins does the person has at the end of this process?
15+
16+
Input Format
17+
18+
The first line of input contains an integer T, which denotes the number of test cases.
19+
20+
Each line of test cases contains two integers N and M.
21+
22+
Constraints
23+
24+
1 <= T <= 10
25+
26+
1 <= N <= 10^10
27+
28+
1 <= M <= 10^6
29+
30+
Output Format
31+
32+
For each test case, in a separate line, output the number of coins that the person has after following the steps mentioned in the problem statement.
33+
34+
Sample Input 0
35+
36+
3
37+
1 5
38+
2 3
39+
5 5
40+
Sample Output 0
41+
42+
5
43+
4
44+
5
45+
Explanation 0
46+
47+
Case 2: The person starts at position 1. He collects coin 1. 2%1 == 0 and 3%1 == 0. So, he throws away the collected coin.
48+
49+
Next, he goes to 2. 3%2 != 0. He has 1 coin.
50+
51+
Next, he collects the 3 remaining coins. So, total coins = 4.
52+
'''
53+
import math
54+
55+
def sumOfDigitsFrom1ToN(n) :
56+
if (n<10) :
57+
return (n*(n+1)/2)
58+
d = (int)(math.log10(n))
59+
a = [0] * (d + 1)
60+
a[0] = 0
61+
a[1] = 45
62+
for i in range(2, d+1) :
63+
a[i] = a[i-1] * 10 + 45 * (int)(math.ceil(math.pow(10,i-1)))
64+
p = (int)(math.ceil(math.pow(10, d)))
65+
msd = n//p
66+
return (int)(msd * a[d] + (msd*(msd-1) // 2) * p +
67+
msd * (1 + n % p) + sumOfDigitsFrom1ToN(n % p))
68+
69+
def compute_hcf(x, y):
70+
while(y):
71+
x, y = y, x % y
72+
return x
73+
74+
t = int(input())
75+
while(t):
76+
t-=1
77+
arr = []
78+
# arr = list(map(int,input().split()))
79+
# a = compute_hcf(arr[0], arr[1])
80+
# _a = sumOfDigitsFrom1ToN(a)
81+
# _a1 = sumOfDigitsFrom1ToN(arr[0])
82+
83+
# if int(_a1 - _a) == 0:
84+
# print(int(arr[1]))
85+
# else:
86+
#print((int(_a1 - _a)-1)+int(arr[1]))
87+
88+
arr = list(map(int,input().split()))
89+
a = compute_hcf(arr[0], arr[1])
90+
print(int((arr[0] - a) + arr[1]))
91+
92+
93+

Gen2_0_PP/Contest/findTheWorkshop.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#include<bits/stdc++.h>
3+
#include<algorithm>
4+
using namespace std;
5+
#define int long long int
6+
#define vi vector<int>
7+
int32_t main()
8+
{
9+
ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
10+
int n,m;
11+
cin>>n>>m;
12+
vi group(n);
13+
for(int i=0;i<n;i++) cin>>group[i];
14+
vi pref(n);
15+
pref[0]=group[0];
16+
for(int i=1;i<n;i++)
17+
{
18+
pref[i]= pref[i-1]+group[i];
19+
}
20+
while(m--)
21+
{
22+
int x;cin>>x;
23+
auto lb= lower_bound(pref.begin(),pref.end(),x);
24+
int len= lb-pref.begin();
25+
cout<<len+1<<" "<<*lb-x<<endl;
26+
}
27+
28+
}
29+
30+
31+
32+
33+
34+
# function to find cumulative sum of array
35+
from itertools import accumulate
36+
prfixsim = []
37+
def cumulativeSum(input):
38+
print ("Sum :", list(accumulate(input)))
39+
40+
# Driver program
41+
if __name__ == "__main__":
42+
input = [4, 6, 12]
43+
cumulativeSum(input)
44+
(list(accumulate(input)))

Gen2_0_PP/Contest/findTheWorkshop2.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
Programming Pathshala has launched N workshops. Each of the courses have a seat limit represented by an array of integers a[i]
3+
4+
To segregate students into batches for these workshops, they have conducted a contest. The students with ranks 1 to a[0] will be a part of the first workshop, those with ranks between a[0] + 1 and a[0] + a[1] will be a part of the second workshop and so on.
5+
6+
Given rank of M different students, for each of them, find the workshop number and his/her rank in that workshop batch.
7+
8+
For example, if the limits of workshops are represented by [30, 20, 30, 10], if a person has rank 40, he will be a part of the second workshop and will have 10th rank in the workshop.
9+
10+
Note that the rank will always be within the maximum number of seats in the workshop.
11+
12+
Input Format
13+
14+
The first line of input consists of two integers N and M, the number of workshops and the number of queries.
15+
16+
The next line of input consists of N space separated integers, denoting the number of seats in each of the workshop, array a[i].
17+
18+
The next line of input consists of M space separated integers, denoting the ranks of the student for which we have queried, represented by array b[i].
19+
20+
Constraints
21+
22+
1 <= N, M <= 200000
23+
24+
1 <= a[i] <= 10^10
25+
26+
1 <= b[i] <= sum(a[i])
27+
28+
Output Format
29+
30+
For each of the M queries, output 2 space separated integers in a new line denoting the workshop number and the person's rank in the workshop.
31+
32+
Sample Input 0
33+
34+
4 2
35+
30 20 30 10
36+
85 40
37+
Sample Output 0
38+
39+
4 5
40+
2 10
41+
Explanation 0
42+
43+
In this case, students with rank (1, 30) will be in first group.
44+
45+
Those with ranks in (31, 50) will be in second group.
46+
47+
Those with ranks in (51, 80) will be in third group.
48+
49+
Those with ranks in (81, 90) will be in fourth group
50+
'''
51+
from itertools import accumulate
52+
from bisect import bisect_right, bisect_left
53+
54+
input1 = list(map(int,input().split()))
55+
input2 = list(map(int,input().split()))
56+
57+
prefixSm = []
58+
prefixSm = (list(accumulate(input2)))
59+
60+
input3 = list(map(int,input().split()))
61+
62+
def findLowerbound(a, x):
63+
i = bisect_left(a, x)
64+
if i != len(a):
65+
if i == 0:
66+
return i+1 , x
67+
else:
68+
return i+1 , x - a[i-1]
69+
70+
#print("prefix sum", prefixSm)
71+
for i in range(len(input3)):
72+
workshopNo , rank = findLowerbound(prefixSm, input3[i])
73+
print(workshopNo, rank)
74+
75+
76+

Gen2_0_PP/Contest/question4.py

-40
This file was deleted.

Gen2_0_PP/Contest/ratingTrouble.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
There has been a recent codeforces contest that the students of the Genesis 1.0 have attempted.
3+
4+
After the contest, they are trying to predict their ratings. There has been a hack in the codeforces server that will lead to the rating of every person after the contest getting updated to the closest prime number to their rating before the contest.
5+
6+
In case, there are multiple ratings closest to the initial rating that are prime, the hacker has ensured that the rating will be updated to the lowest one.
7+
8+
Can you help the participants to determine their rating after the contest?
9+
10+
Input Format
11+
12+
The first line of input contains T, the number of students who wants to calculate their ratings.
13+
14+
The next T lines contain integer N, rating of the person.
15+
16+
Constraints
17+
18+
1 <= T <= 1000
19+
20+
1 <= N <= 1000000
21+
22+
Output Format
23+
24+
Output T lines, each line containing the result of the ith query.
25+
26+
Sample Input 0
27+
28+
3
29+
10
30+
20
31+
30
32+
Sample Output 0
33+
34+
11
35+
19
36+
29
37+
Explanation 0
38+
39+
For the first test case, since the number 11 is closer to 10, than 7 (|11-10|<|10-7|), thus 11 is the correct answer.
40+
41+
Simlilarly for second test case.
42+
43+
For the third test case, since 2 closest primes are possible (viz. 29 and 31), we report the smaller one i.e 29.
44+
45+
Submissions: 38
46+
Max Score: 100
47+
Difficulty: Medium
48+
Rate This Challenge:
49+
50+
51+
More
52+
53+
'''
54+
def getPrimes(limit):
55+
primes = []
56+
numbers = [True] * limit
57+
for i in range(2, limit):
58+
if numbers[i]:
59+
primes.append(i)
60+
for n in range(i ** 2, limit, i):
61+
numbers[n] = False
62+
return primes
63+
64+
65+
t = int(input())
66+
while(t):
67+
t-=1
68+
number = int(input())
69+
primes = getPrimes(number + 100)
70+
maxDist = 99999999
71+
numb = 0
72+
for p in primes:
73+
if abs(number - p) < maxDist:
74+
maxDist = abs(number - p)
75+
numb = p
76+
77+
print(numb)

0 commit comments

Comments
 (0)