├── .gitignore ├── ALGORITHM ├── Graph Representation │ ├── edgeExistence_TEST.cpp │ ├── monkAtTheGraphFactory_E.cpp │ └── monkInTheRealEstate_E.cpp ├── Hamiltonian Path │ ├── fredo'sCrush_M.cpp │ ├── fredoAndHisBirthdayGift_M.cpp │ ├── microAndCoins_VE.cpp │ └── microAndPermutations_VE.cpp └── Searching │ └── Linear Search │ ├── holidaySeason_M.cpp │ ├── repeatedKTimes_M.cpp │ └── specialShop_M.cpp ├── BASIC PROGRAMMING ├── Basics of Bit Manipulation │ ├── ANDSum_E.cpp │ ├── MYSTERY_E.cpp │ ├── XORAndProject_E.cpp │ ├── aaryanSubsequencesAndGreatXOR_E.cpp │ ├── aishAndXOR_M.cpp │ ├── and&And_M.cpp │ ├── bitFlippingGame_E.cpp │ ├── carInTheLane_E.cpp │ ├── chandanAndBalancedStrings_H.cpp │ ├── countTheBits_TEST.cpp │ ├── escapeTheNight_E.cpp │ ├── flashVsKatmos_E.cpp │ ├── gameOfDeletion_M.cpp │ ├── haveYouMetTed_M.cpp │ ├── helpYourRoommate_E.cpp │ ├── hihiAndCrazyBits_VE.cpp │ ├── iDemandATrialByCombat_M.cpp │ ├── jarvisAndLoneInteger_E.cpp │ ├── lazyPanda_E.cpp │ ├── luckyNumbers_E.cpp │ ├── matteyMultiplication_M.cpp │ ├── maximizingExpressions_M.cpp │ ├── maximumAND_E.cpp │ ├── microAndBinaryStrings_E.cpp │ ├── monkAndBinaryArray_M.cpp │ ├── monkAndHisFather_E.cpp │ ├── monkAndHisFriend_E.cpp │ ├── monkAndTasks_E.cpp │ ├── monkAndTheBoxOfCookies_VE.cpp │ ├── monksChoiceOfNumbers_E.cpp │ ├── monstersInGrid_M.cpp │ ├── mystery_VE.cpp │ ├── naviAndMath_M.cpp │ ├── pikachuVsTeamMeowsticAndHelpingHand_E.cpp │ ├── playerOfNextMatch_E.cpp │ ├── poisonousGas_E.cpp │ ├── powerOf2_E.cpp │ ├── rajanAndOddFrequencyNumber_E.cpp │ ├── samuAndHerBirthdayParty_M.cpp │ ├── setAndUnsetBits_E.cpp │ ├── sherlockAndXOR_E.cpp │ ├── specialBitNumbers_E.cpp │ ├── subArrayFunctions_E.cpp │ ├── subhamAndBinaryStrings_VE.cpp │ ├── subsetAND_E.cpp │ ├── substringQueries_M.cpp │ ├── sumOfNumbers_E.cpp │ ├── theCastleGate_VE.cpp │ ├── theGameOfOXA_E.cpp │ ├── theLazyHash_E.cpp │ ├── toTryOrNotToTry_E.cpp │ ├── tryMeFirst_E.cpp │ ├── viserionAndBran_E.cpp │ ├── xorAndPrimeQueries_M.cpp │ ├── xorRectangle_M.cpp │ └── xsquareAndTwoStrings_E.cpp ├── Basics of IO │ ├── aliAndHelpingInnocentPeople_E.cpp │ ├── aman&Mr.Sharma_E.cpp │ ├── anagrams_VE.cpp │ ├── bestIndex_M.cpp │ ├── bookOfPotionMaking_E.cpp │ ├── bricksGame_E.cpp │ ├── charSum_E.cpp │ ├── cipher_M.cpp │ ├── conject-It!_E.cpp │ ├── costOfBalloons_VE.cpp │ ├── countDivisors_VE.cpp │ ├── duration_E.cpp │ ├── e-maze-in_E.cpp │ ├── factorial!_VE.cpp │ ├── findProduct_VE.cpp │ ├── friendsRelationship_E.cpp │ ├── gokiAndHisBreakup_E.cpp │ ├── hello_E.cpp │ ├── ladderophilia_E.cpp │ ├── lifeTheUniverseAndEverything_E.cpp │ ├── liftQueries_E.cpp │ ├── magicalWord_M.cpp │ ├── minimizeCost_M.cpp │ ├── palindromicString_VE.cpp │ ├── playWithNumbers_E.cpp │ ├── primeNumber_VE.cpp │ ├── royAndProfilePicture_VE.cpp │ ├── seatingArrangement_VE.cpp │ ├── sevenSegmentDisplay_E.cpp │ ├── splitHouses_VE.cpp │ ├── toggleString_VE.cpp │ └── twoStrings_E.cpp ├── Basics of Implementation │ ├── anEquilateralTriangle_E.cpp │ ├── arrayInsert_VE.cpp │ ├── batmanAndTickTackToe_VE.cpp │ ├── capitalsAndCities_M.cpp │ ├── cityTravel_VE.cpp │ ├── countDigits_TEST.cpp │ ├── deletingNumbers_E.cpp │ ├── digitProblem_VE.cpp │ ├── printHackerearth_VE.cpp │ └── theGreatKian_VE.cpp ├── Basics of Operators │ ├── birthdayParty_E.cpp │ ├── letUsUnderstandComputer_E.cpp │ └── yetAnotherPartitionProblem_M.cpp ├── Complexity Analysis │ ├── A+B_E.cpp │ └── vowelRecognition_M.cpp └── Recursion and Backtracking │ ├── GCDStrings_E.cpp │ ├── N-Queens_E.cpp │ ├── N-Queens_TEST.cpp │ ├── aTrystWithChess_E.cpp │ ├── biggestForest_E.cpp │ ├── converting_E.cpp │ ├── divideNumber_M.cpp │ ├── itsConfidential_E.cpp │ ├── momentsInArrays_H.cpp │ └── simranAndStairs_E.cpp ├── DATA STRUCTURES ├── 1-D │ ├── 2Arrays_E.cpp │ ├── B)TeamSelection_E.cpp │ ├── EEDCLab_E.cpp │ ├── R-r-riddikulus!OnceAgain_E.cpp │ ├── accheDin_E.cpp │ ├── anshulUsamaAndPunishment-A_E.cpp │ ├── arrayGame_E.cpp │ ├── biasedChandan_E.cpp │ ├── binaryQueries_VE.cpp │ ├── canYouSolveIt_M.cpp │ ├── chargedUpArray_E.cpp │ ├── dedicationLevel=Infinity_E.cpp │ ├── digitalSequence_E.cpp │ ├── findingTheSubarrays_E.cpp │ ├── hamiltonianAndLagrangian_E.cpp │ ├── helpJarvis!_E.cpp │ ├── infiniteArrays_E.cpp │ ├── longATMQueue_E.cpp │ ├── markTheAnswer_E.cpp │ ├── maximizeTheEarning_E.cpp │ ├── maximumGoodness_E.cpp │ ├── maximumOfK-SizeSubarrays_E.cpp │ ├── memoriseMe!_VE.cpp │ ├── microAndArrayUpdate_VE.cpp │ ├── modifySequence_E.cpp │ ├── monkAndLuckyMinimum_E.cpp │ ├── monkAndPowerOfTime_E.cpp │ ├── monkAndRotation_E.cpp │ ├── monkAndWelcomeProblem_VE.cpp │ ├── neutralisationOfCharges_E.cpp │ ├── notInRange_E.cpp │ ├── pairs_E.cpp │ ├── pepperAndContiguousEvenSubarray_E.cpp │ ├── perfectSubarray_E.cpp │ ├── polygonPossibility_E.cpp │ ├── printArrayInReverse_TEST.cpp │ ├── speed_E.cpp │ ├── strangeGame_E.cpp │ └── takeoff_E.cpp └── Multi-dimensional │ └── addAlternateElementsOf2-DimensionalArray_E.cpp ├── MATHS ├── Basic Number Theory-1 │ ├── greatArjitAndNumbers_M.cpp │ ├── helpOz_E.cpp │ ├── loveTriangle_VE.cpp │ ├── luckyStringJackpot_M.cpp │ ├── mancunianAndPandigitalNumbers_M.cpp │ ├── moguLovesNumbers_E.cpp │ ├── mystery_VE.cpp │ ├── pandaAndChainReaction_E.cpp │ ├── permutationAgain_E.cpp │ ├── rhezoAndBigPowers_M.cpp │ ├── riyasBirthdayParty_E.cpp │ ├── royAndBirthday_E.cpp │ ├── selectionOfCities_E.cpp │ ├── theConfusedMonk_E.cpp │ ├── theEasyOne_E.cpp │ ├── theFinalFight[6]_M.cpp │ ├── ticTacToe_E.cpp │ └── unlockTheDoor_M.cpp ├── Basic Number Theory-2 │ ├── 4141_M.cpp │ ├── akashsGirlfriend_M.cpp │ ├── ashuAndPrimeFactors_E.cpp │ ├── bobAndTheCleverScoop_E.cpp │ ├── ebet_M.cpp │ ├── hackerDecryptingMessages_M.cpp │ ├── holiAndDivisionFunction_M.cpp │ ├── mikeAndGCDIssues_E.cpp │ ├── monkAndDivisibleNumbers_M.cpp │ ├── monkAndQueriesOnCoordinatePlane_M.cpp │ ├── nearestPrime_E.cpp │ ├── numberOfPrimes_TEST.cpp │ ├── numberSummation_M.cpp │ ├── sumOfPrimes_M.cpp │ ├── zoroAndChocolates_M.cpp │ └── zrZr_E.cpp ├── Basics of Combinatorics │ ├── bobAndInternship_E.cpp │ ├── happyAndSets_E.cpp │ ├── hardikAndBoxes_M.cpp │ ├── innocentSwapsAndHisEmotions_M.cpp │ ├── kSpecialCells_E.cpp │ ├── microAndEvenNumbers_E.cpp │ ├── powerSetGame_M.cpp │ ├── shinoAndKSpecialCells_E.cpp │ ├── velmaAndTriplets_E.cpp │ ├── winTheGame_E.cpp │ └── wordRank_M.cpp ├── Inclusion Exclusion │ ├── 47sTask_E.cpp │ ├── InclusionExclusion_TEST.cpp │ ├── andrewAndWengaluruCity_E.cpp │ ├── evenGrid_E(BestSubmission).cpp │ ├── iceCreamKhaloFraaaaandsss_M.cpp │ ├── rheasHomework_E.cpp │ └── telephoneNetwork_E.cpp ├── Primality Test │ ├── anotherPrimeProblem_E.cpp │ ├── coprCopr_M.cpp │ ├── doesItDivide_E.cpp │ ├── helpRachit_M.cpp │ ├── inLoveWithPrimes_E.cpp │ ├── jadooAndPrimes_E.cpp │ ├── littleJhoolAndHisPrimes_M.cpp │ ├── ma5termindsBirthdayParty_E.cpp │ ├── microAndPrimePrime_E.cpp │ ├── nakulAndGoldCoins_E(BestSubmissionProb).cpp │ ├── pairwiseProducts_M(BestSubmission).cpp │ ├── perfectCubes_M.cpp │ ├── primeInterval_E.cpp │ ├── primeMinistersNumber_M.cpp │ ├── primeNumber_TEST.cpp │ ├── primeString_E.cpp │ ├── royAndRangoli_M.cpp │ ├── royAndShopping_E.cpp │ ├── ryotsuLovesPrimes_E.cpp │ ├── saveGirlfriend_E.cpp │ ├── shootoutInLondon_E.cpp │ ├── shwetaRiddhimaAndChocolates-A_E.cpp │ ├── shwetaRiddhimaAndChocolates-B_M.cpp │ ├── smallestNumber_E.cpp │ ├── smithNumbers_M.cpp │ ├── theEulerConjecture_M(BestSubmissionProb).cpp │ ├── theFirstMeeting_E.cpp │ ├── warBegins_M.cpp │ └── weLovePirates!_E.cpp └── Totient Function │ └── EulerTotientFunction_TEST.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | __*.cpp 2 | __*.bat 3 | __*.lnk 4 | __*.exe 5 | __*.o 6 | __*.txt -------------------------------------------------------------------------------- /ALGORITHM/Graph Representation/edgeExistence_TEST.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | vector vec[MX],ans; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 10 | int n,m,k,res=0; cin>>n>>m>>k; 11 | while(m--){ 12 | int a,b; cin>>a>>b; 13 | vec[a].push_back(b); 14 | } 15 | int cur=1; 16 | while(k--){ 17 | int a,b; cin>>a>>b; 18 | if(a==cur) cur=b; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ALGORITHM/Graph Representation/monkAtTheGraphFactory_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 7 | int n,sumOfDegree=0; cin>>n; for(int i=0;i>x; sumOfDegree+=x; 9 | } 10 | cout<<(sumOfDegree==(--n<<1)?"Yes\n":"No\n"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /ALGORITHM/Graph Representation/monkInTheRealEstate_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | unordered_set st; 9 | int e; cin>>e; while(e--){ 10 | int x,y; cin>>x>>y; st.insert(x); st.insert(y); 11 | } 12 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | typedef long double ld; 5 | 6 | int calc(int x) { 7 | int i = 1, res = 0; 8 | while (x) { 9 | if (x & 1) res += i; 10 | x >>= 1; i++; 11 | } 12 | return res; 13 | } 14 | 15 | void solve() { 16 | int n, m, x; cin >> n >> m; x = 1 << n; 17 | if (!m) { cout << n << " " << 1 << "\n"; return; } 18 | vector> vec(x, vector(n, 0)); 19 | vector pathLenght(n, 0), res(n, 0); 20 | map, bool> mpp; 21 | for (int i = 0; i < m; ++i) { 22 | int x, y; cin >> x >> y; --x; --y; 23 | mpp[make_pair(x, y)] = mpp[make_pair(y, x)] = true; 24 | } 25 | for (int i = 0; i < n; ++i) { 26 | vec[1 << i][i] = 1; res[i] = i + 1; 27 | } 28 | for (int i = 0; i < x; ++i) { 29 | int pL = __builtin_popcount(i), r = calc(i); 30 | for (int j = 0; j < n; ++j) { 31 | if (i & (1 << j)) { 32 | for (int k = 0; k < n; ++k) { 33 | if (k != j and i & (1 << k) and mpp[make_pair(k, j)] and vec[i ^ (1 << j)][k]) { 34 | vec[i][j]++; 35 | if (pathLenght[j] <= pL) { 36 | pathLenght[j] = pL; 37 | res[j] = max(res[j], r); 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } 44 | // for (auto r : res) cout << r << " "; cout << "\n"; 45 | int mx = *max_element(res.begin(), res.end()), mn = *min_element(res.begin(), res.end()); 46 | int gcd = __gcd(mn, mx); 47 | cout << mx / gcd << " " << mn / gcd << "\n"; 48 | // for (auto row : vec) { for (auto val : row) cout << val << " "; cout << "\n"; } 49 | // cout << accumulate(vec[x - 1].begin(), vec[x - 1].end(), 0); 50 | } 51 | 52 | int main() { 53 | ios_base::sync_with_stdio(false); cin.tie(NULL); 54 | #ifndef ONLINE_JUDGE 55 | freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 56 | #endif 57 | int t; cin >> t; while (t--) { 58 | solve(); 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /ALGORITHM/Hamiltonian Path/fredoAndHisBirthdayGift_M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | typedef long double ld; 5 | 6 | void solve() { 7 | int n, m, x; cin >> n >> m; x = 1 << n; 8 | vector res(n, 0); 9 | vector> vec(x, vector(n, 0)); 10 | map, bool> mpp; 11 | for (int i = 0; i < m; ++i) { 12 | int x, y; cin >> x >> y; --x; --y; 13 | mpp[make_pair(x, y)] = mpp[make_pair(y, x)] = true; 14 | } 15 | for (int i = 0; i < n; ++i) { 16 | vec[1 << i][i] = true; 17 | } 18 | for (int i = 0; i < x; ++i) { 19 | int len = __builtin_popcount(i); 20 | for (int j = 0; j < n; ++j) { 21 | if (i & (1 << j)) { 22 | for (int k = 0; k < n; ++k) { 23 | if (k != j and (i & (1 << k)) and mpp[make_pair(k, j)] and vec[i ^ (1 << j)][k]) { 24 | vec[i][j] = true; 25 | res[j] = max(res[j], len - 1); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | for (auto r : res) cout << r << " "; cout << "\n"; 32 | // int i = 0; for (auto row : vec) { cout << __builtin_popcount(i++) << "\t"; for (auto val : row) cout << val << " "; cout << "\n"; } 33 | } 34 | 35 | int main() { 36 | ios_base::sync_with_stdio(false); cin.tie(NULL); 37 | #ifndef ONLINE_JUDGE 38 | freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 39 | #endif 40 | int t; cin >> t; while (t--) { 41 | // cout << (solve() ? "YES\n" : "NO\n"); 42 | // cout << solve() << "\n"; 43 | solve(); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /ALGORITHM/Hamiltonian Path/microAndCoins_VE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | typedef long double ld; 5 | 6 | bool solve() { 7 | int n, m, x; cin >> n >> m; x = 1 << n; 8 | vector> vec(x, vector(n, 0)); 9 | map, bool> mpp; 10 | for (int i = 0; i < m; ++i) { 11 | int x, y; cin >> x >> y; --x; --y; 12 | mpp[make_pair(x, y)] = mpp[make_pair(y, x)] = true; 13 | } 14 | for (int i = 0; i < n; ++i) { 15 | vec[1 << i][i] = 1; 16 | } 17 | for (int i = 0; i < x; ++i) { 18 | for (int j = 0; j < n; ++j) { 19 | if (i & (1 << j)) { 20 | for (int k = 0; k < n; ++k) { 21 | if (k != j and i & (1 << k) and mpp[make_pair(k, j)]) { 22 | if (vec[i ^ (1 << j)][k]) { 23 | vec[i][j] = true; 24 | } 25 | } 26 | } 27 | } 28 | } 29 | } 30 | for (auto v : vec[x - 1]) if (v) return true; 31 | return false; 32 | } 33 | 34 | int main() { 35 | ios_base::sync_with_stdio(false); cin.tie(NULL); 36 | #ifndef ONLINE_JUDGE 37 | freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 38 | #endif 39 | int t; cin >> t; while (t--) { 40 | cout << (solve() ? "Yes\n" : "No\n"); 41 | // cout << solve() << "\n"; 42 | // solve(); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /ALGORITHM/Hamiltonian Path/microAndPermutations_VE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | typedef long double ld; 5 | 6 | void solve() { 7 | int n, m, x; cin >> n >> m; x = 1 << n; 8 | vector> vec(x, vector(n, 0)); 9 | map, bool> mpp; 10 | for (int i = 0; i < m; ++i) { 11 | int x, y; cin >> x >> y; --x; --y; 12 | mpp[make_pair(x, y)] = mpp[make_pair(y, x)] = true; 13 | } 14 | for (int i = 0; i < n; ++i) { 15 | vec[1 << i][i] = 1; 16 | } 17 | for (int i = 0; i < x; ++i) { 18 | for (int j = 0; j < n; ++j) { 19 | if (i & (1 << j)) { 20 | for (int k = 0; k < n; ++k) { 21 | if (k != j and i & (1 << k) and mpp[make_pair(k, j)]) { 22 | vec[i][j] += vec[i ^ (1 << j)][k]; 23 | } 24 | } 25 | } 26 | } 27 | } 28 | cout << accumulate(vec[x - 1].begin(), vec[x - 1].end(), 0); 29 | } 30 | 31 | int main() { 32 | ios_base::sync_with_stdio(false); cin.tie(NULL); 33 | #ifndef ONLINE_JUDGE 34 | freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 35 | #endif 36 | solve(); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /ALGORITHM/Searching/Linear Search/holidaySeason_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int SZ = 26; 6 | 7 | int mpp[SZ]; 8 | 9 | int main () { 10 | string str; int n; cin >> n >> str; 11 | ll res = 0, cur = 0; 12 | for (int i = 0; i < n; ++i) { 13 | for (int j = i + 1, cur = 0; j < n; ++j) { 14 | if (str[i] == str[j]) { 15 | res += cur; 16 | } 17 | cur += mpp[str[j] - 'a']; 18 | } 19 | mpp[str[i] - 'a']++; 20 | } 21 | cout << res; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ALGORITHM/Searching/Linear Search/repeatedKTimes_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main () { 6 | int n; cin >> n; 7 | unordered_map mpp; 8 | vector vec(n, -1); 9 | for (int i = 0; i < n; ++i) { 10 | cin >> vec[i]; 11 | mpp[vec[i]]++; 12 | } 13 | int k, mn = 1e9; cin >> k; 14 | for (int i = 0; i < n; ++i) { 15 | if (mpp[vec[i]] == k and vec[i] < mn) mn = vec[i]; 16 | } 17 | cout << mn; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ALGORITHM/Searching/Linear Search/specialShop_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | ll n, a, b; 7 | 8 | ll sqr(ll x) { return x * x; } 9 | ll calc(ll x) { return (a * sqr(x)) + (b * sqr(n - x)); } 10 | 11 | ll findMin() { 12 | ll l = 0, r = n, mx = 1e17; 13 | while(l <= r) { 14 | ll mid = l + (r - l) / 2, x = calc(mid); 15 | if (x < calc(mid + 1)) { 16 | mx = x; 17 | r = mid - 1; 18 | } else { 19 | l = mid + 1; 20 | } 21 | } 22 | return mx; 23 | } 24 | 25 | int main () { 26 | // cin >> n >> a >> b; for (int i = -1; i < 2 * n; ++i) cout << i << "\t" << calc(i) << "\n"; 27 | ll t; cin >> t; while (t--) { 28 | cin >> n >> a >> b; cout << findMin() << "\n"; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Bit Manipulation/ANDSum_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const ll MX = 32; 6 | const ll MOD = 1e9+7; 7 | 8 | ll power(ll x,ll y){ 9 | ll res=1; 10 | while(y>0){ 11 | if(y%2==0){ y/=2; x=(x*x)%MOD; } 12 | else { y-=1; res=(res*x)%MOD; y/=2; x=(x*x)%MOD; } 13 | } 14 | return res; 15 | } 16 | 17 | ll arr[MX]; 18 | 19 | int main(){ 20 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 21 | ll t; cin>>t; while(t--){ 22 | memset(arr, 0, sizeof(arr)); 23 | ll res=0,n; cin>>n; while(n--){ 24 | ll x; cin>>x; for(ll i=0;i<32;++i) if(x&(1< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | long long n; while(cin>>n){ cout<<(n&-n)< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int n,x=0; cin>>n; for(;n;n>>=1) x^=n; 8 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int n,res=0; cin>>n; while(n--){ int x; cin>>x; res|=x; } 8 | cout< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int nb[MX+1],XOR[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int n,q,x; cin>>n; 11 | for(int i=1;i<=n;++i){ 12 | cin>>x; XOR[i]=x^XOR[i-1]; nb[i]=(!x)+nb[i-1]; 13 | } 14 | cin>>q; while(q--){ 15 | int l,r; cin>>l>>r; cout<<(XOR[r]^XOR[l-1])<<" "<<(nb[r]-nb[l-1])< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | map mpp = {{ '0' , 0 },{ '1' , 1 },{ '2' , 2 },{ '3' , 3 },{ '4' , 4 },{ '5' , 5 },{ '6' , 6 },{ '7' , 7 },{ '8' , 8 },{ '9' , 9 },{ 'A' , 10 },{ 'B' , 11 },{ 'C' , 12 },{ 'D' , 13 },{ 'E' , 14 },{ 'F' , 15 },{ 'G' , 16 },{ 'H' , 17 },{ 'I' , 18 },{ 'J' , 19 },{ 'K' , 20 },{ 'L' , 21 },{ 'M' , 22 },{ 'N' , 23 },{ 'O' , 24 },{ 'P' , 25 },{ 'Q' , 26 },{ 'R' , 27 },{ 'S' , 28 },{ 'T' , 29 },{ 'U' , 30 },{ 'V' , 31 },{ 'W' , 32 },{ 'X' , 33 },{ 'Y' , 34 },{ 'Z' , 35 },{ 'a' , 36 },{ 'b' , 37 },{ 'c' , 38 },{ 'd' , 39 },{ 'e' , 40 },{ 'f' , 41 },{ 'g' , 42 },{ 'h' , 43 },{ 'i' , 44 },{ 'j' , 45 },{ 'k' , 46 },{ 'l' , 47 },{ 'm' , 48 },{ 'n' , 49 },{ 'o' , 50 },{ 'p' , 51 },{ 'q' , 52 },{ 'r' , 53 },{ 's' , 54 },{ 't' , 55 },{ 'u' , 56 },{ 'v' , 57 },{ 'w' , 58 },{ 'x' , 59 },{ 'y' , 60 },{ 'z' , 61 },{ '-' , 62 },{ '_' , 63 },}; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | int t; cin>>t; while(t--){ 12 | ll cnt=0,res=1; string str; cin>>str; 13 | for(char s:str) cnt+=(6-__builtin_popcount(mpp[s])); 14 | while(cnt--) res=((res<<1)+res)%MOD; 15 | cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | string n(MX,'0'); 9 | int t; cin>>t; while(t--){ 10 | string str; cin>>str; for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll n; cin>>n; cout<<(1<<__builtin_popcountll(n))< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main() { 7 | int t; cin>>t; while(t--){ 8 | unordered_map mpp; string str; cin>>str; ll x=0,res=0; mpp[x]=1; 9 | for(auto s:str){ 10 | x^=(1<<(s-'a')); 11 | res+=mpp[x]; 12 | mpp[x]++; 13 | } 14 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ int n; cin>>n; cout<<__builtin_popcount(n)< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | int t; cin>>t; while(t--){ 10 | ll n,res=0; cin>>n; 11 | for(ll i=1;(1LL< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; while(t--){ 11 | int n; cin>>n; 12 | for(int i=0;i>arr[i]; 13 | stable_sort(arr,arr+n,[](int a, int b){return __builtin_popcount(a)<__builtin_popcount(b);}); 14 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | 8 | int n,or1=0,or2=0,s1=0,s2=0; cin>>n; 9 | for(int i=0;i>x; or1|=x; s1+=x; } 10 | for(int i=0;i>x; or2|=x; s2+=x; } 11 | 12 | int r1 = (s1-or1),r2 = (s2-or2); 13 | (r1>r2)?cout<<"1 "<<(r1-r2):cout<<"2 "<<(r2-r1); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Bit Manipulation/haveYouMetTed_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int n,res=1e9; cin>>n; while(n--){ int x; cin>>x; res=min(res,__builtin_popcount(x)); } 9 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int x,res=0; cin>>x; while(x){ res++, x&=(x-1); } 9 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll x; cin>>x; cout<<(x|(~x&(x+1)))< 3 | using namespace std; 4 | const int BITS = 30; 5 | const int MX = 1e5; 6 | typedef long long ll; 7 | 8 | ll arr[MX+1][BITS]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 12 | int n,q; cin>>n>>q; for(int i=1;i<=n;++i){ 13 | int x; cin>>x; for(int j=0;j<30;++j) arr[i][j]=arr[i-1][j]+(1&(x>>j)); 14 | } 15 | for(int i=0;i>l>>r>>x; tot=r-l+1; 17 | for(ll j=0;j<30;++j) res+=(1L< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll n,res=0; cin>>n; while(n--){ ll x; cin>>x; res^=x; } 10 | cout<<(res?res:-1)< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll n; cin>>n; cout<<__builtin_popcountll(n)< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | int t; cin>>t; while(t--){ 10 | ll n,res=0; cin>>n; 11 | for(ll i=2;i<=n;i<<=1){ 12 | for(ll j=1;j 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | bool flag=false; ll n,m; cin>>n>>m; 10 | for(ll i=int(log2(m));i>=0;--i){ 11 | if(m&(1L< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e5; 6 | 7 | ll arr[MX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | ll res=0; int n; cin>>n; 12 | for(int i=0;i>arr[i]; 13 | for(int i=0;i>x; arr[i]^=x; } 14 | for(int i=0;i>x; res+=(~arr[i]&x)?(arr[i]^(~arr[i]&x)):(arr[i]^(x&-x)); } 15 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll a,b; cin>>a>>b; (b-a==1)?cout<<(b&(b-1))< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ int n; string str; cin>>n>>str; cout< 3 | using namespace std; 4 | const int MX = 2000; 5 | 6 | int arr[MX],x0[MX+1],x1[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n,tot=0; cin>>n; 11 | for(int i=0;i>arr[i]; 12 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ ll n; cin>>n; cout<<__builtin_popcountll(n)< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll m,p; cin>>m>>p; cout<<(__builtin_popcountll(m^p))< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; while(t--){ 11 | int n; cin>>n; 12 | for(int i=0;i>arr[i]; 13 | stable_sort(arr,arr+n,[](int a, int b){return __builtin_popcount(a)<__builtin_popcount(b);}); 14 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int arr[32]={0},n; cin>>n; while(n--){ 9 | int x; cin>>x; for(int i=0;i<32;++i){ 10 | if(x&(1< 3 | using namespace std; 4 | const int MX = 1e3; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; while(t--){ 11 | int n,k; cin>>n>>k; 12 | for(int i=0;i>x; arr[i]=__builtin_popcount(x); } 13 | stable_sort(arr,arr+n,[](int a, int b){return a>b;}); 14 | cout< 3 | using namespace std; 4 | const int MX = 20; 5 | 6 | int arr[MX][MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int n,m,k,res=0; cin>>n>>m>>k; 11 | for(int i=0;i>arr[i][j]; 12 | for(int i=0;i<(1<>b)&1 and arr[a][b]==1) mxlist[a]++; 16 | sort(mxlist, mxlist+n, greater()); 17 | res = max(res, accumulate(mxlist,mxlist+min(l,n),0)); 18 | } 19 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | long long n; while(cin>>n) cout<<__builtin_popcountll(n)<<'\n'; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Bit Manipulation/naviAndMath_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 16; 7 | 8 | ll arr[MX]; 9 | 10 | ll power(ll x, ll y){ 11 | ll res=1; 12 | while(y>0){ 13 | if(y&1) res=(res*x)%MOD; 14 | y>>=1, x=(x*x)%MOD; 15 | } 16 | return res; 17 | } 18 | 19 | int main(){ 20 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 21 | int t,n; cin>>t; for(int testcase=1;testcase<=t;++testcase){ 22 | ll res=0,resMul=1,resAdd=0; cin>>n; 23 | for(int i=0;i>arr[i]; 24 | for(int i=2;i<(1< res ? temp : res; 33 | } 34 | cout<<"Case #"< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int n,k; cin>>n>>k; k=min(96,k); 11 | for(int i=0;i>arr[i]; 12 | for(int i=0;i=i+1;--j) arr[j]|=arr[j-1]; 13 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int n,res=0; cin>>n; while(n--){ 9 | int x; cin>>x; if(x) res+=(1< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int n,tot=0; cin>>n; while(n--){ int x; cin>>x; if(x>0) tot+=x; } 9 | (tot and (tot&(tot-1))==0)?cout<<"Yes\n":cout<<"No\n"; 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Bit Manipulation/powerOf2_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int n,res=0; cin>>n; while(n--){ ll x; cin>>x; if((x&(x-1))==0) res++; } 9 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int n,x,res=0; cin>>n; n=(n<<1)+1; while(n--){ cin>>x; res^=x; } 8 | cout< 3 | using namespace std; 4 | 5 | bool bitcompare(int x, int y){ return (__builtin_popcount(x) < __builtin_popcount(y)); } 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int t; cin>>t; while(t--){ 10 | int n,k,mask,res=11; cin>>n>>k; int arr[n]; 11 | for(int i=0;i>str; arr[i]=stoll(str,0,2); } 12 | sort(arr, arr+n, bitcompare); 13 | for(int i=1;i<(1< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 627509; 6 | 7 | int arr[MX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | for(int i=1,cnt=0;cnt>t; while(t--){ 13 | int n; cin>>n; cout< 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | int n; ll odd=0,even=0; cin>>n; while(n--){ 10 | int x; cin>>x; (x&1)?(odd++):(even++); 11 | } 12 | cout< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int n,q; cin>>n>>q; for(int i=1;i<=n;++i){ int x; cin>>x; arr[i]=arr[i-1]+bool(x&(x>>1)); } 11 | while(q--){ int l,r; cin>>l>>r; cout< 3 | using namespace std; 4 | 5 | int main() { 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,m,p; cin>>n>>m>>p; 9 | vector a(n); 10 | 11 | for(int i=0;i>a[i]; 12 | 13 | int l=-1,r=-1,res=0; 14 | 15 | for(int i=0;i pqmax; 17 | priority_queue,greater> pqmin; 18 | pqmax.push(a[i]); pqmin.push(a[i]); 19 | 20 | int ilimit = min(p,m)-1; 21 | if(ilimit+i>n) break; 22 | 23 | int func1=a[i],func2=a[i],maxFunc3=INT_MIN,maxJ=-1; 24 | 25 | for(int j=i+1;ja[j]){ 31 | func1^=pqmax.top(); 32 | func1^=a[j]; 33 | pqmax.pop(); 34 | pqmax.push(a[j]); 35 | } 36 | 37 | if(pqmin.size() 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | string str; int n,cnt=0; cin>>n>>str; 9 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int f=0,z,n,x; cin>>z>>n; while(n--){ int x; cin>>x; if(!(z&=x)) f=1; } 9 | (f)?cout<<"Yes\n":cout<<"No\n"; 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Bit Manipulation/substringQueries_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int val(char c){ 7 | if(c>='a') return 10+26+c-'a'; 8 | if(c>='A') return 10+c-'A'; 9 | return c-'0'; 10 | } 11 | 12 | int arr[62]; 13 | 14 | int main() { 15 | int t; cin>>t; while(t--){ 16 | string str; cin>>str; 17 | int n=str.size(); 18 | int q; cin>>q; while(q--){ 19 | string sstr; cin>>sstr; 20 | ll mask=0,test=0,res=0; for(auto ss:sstr) mask|=(1L< 3 | using namespace std; 4 | const int MX = 15; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; while(t--){ 11 | int n,s,res; 12 | cin>>n; for(int i=0;i>arr[i]; 13 | cin>>s; for(int i=0;i<(1< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int n,res=0; cin>>n; 9 | for(int i=2;i<=n;++i) for(int j=1;j 3 | using namespace std; 4 | const int MX = 16; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; 11 | while(t--){ 12 | string predict; cin>>predict; int ans,k,n,mx=0; cin>>n; 13 | for(int i=0;i>arr[i]; 14 | for(int i=0;i<(1< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | int t; cin>>t; while(t--){ 9 | ll n; cin>>n; cout<<__builtin_popcountll(n)< 3 | using namespace std; 4 | #define M 150 5 | 6 | bitset reset,arr[20]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t,n,m,x; cin>>t; while(t--){ 11 | int cnt=0; cin>>n>>m; 12 | for(int i=0;i=0;--j){ cin>>x; if(x){ bitset tmp; tmp.set(j); arr[i]|=tmp; } } 13 | for(int i=0;i<(1< res; for(int j=0;j 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; cin>>n; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | int t; cin>>t; while(t--){ 10 | ll res=1; string str; cin>>str; for(int i=1;i 3 | using namespace std; 4 | typedef long long ll; 5 | #define pc putchar 6 | const ll MX = 1e6; 7 | const ll inf = 1e7; 8 | 9 | void in (ll &x){ 10 | bool neg = false; 11 | register int c; 12 | x = 0; 13 | c = getchar(); 14 | if(c == '-'){ 15 | neg = true; 16 | c = getchar(); 17 | } 18 | for(; (c>47 && c<58); c = getchar()) 19 | x = x * 10 + c - 48; 20 | if(neg) 21 | x *= -1; 22 | } 23 | void out(ll x){ 24 | ll n = x, rem, ct = 0; 25 | rem = n; 26 | if(n == 0){ 27 | pc('0'); 28 | return; 29 | } 30 | while ((rem%10) == 0) { 31 | ++ct; 32 | rem /= 10; 33 | } 34 | rem = 0; 35 | while (n) { 36 | rem = rem * 10 + n%10; 37 | n /= 10; 38 | } 39 | while (rem) { 40 | pc(rem%10 + '0'); 41 | rem /= 10; 42 | } 43 | while (ct--) { 44 | pc('0'); 45 | } 46 | } 47 | 48 | bool prime[MX+10]; 49 | ll parr[MX+10],pidx[MX+10],pnext[MX+10],pprev[MX+10],idx; 50 | 51 | int main(){ 52 | memset(prime,true,sizeof(prime)); 53 | prime[0]=prime[1]=false; 54 | for(int i=2;i*i<=MX;++i) if(prime[i]) for(int j=i*i;j<=MX;j+=i) prime[j]=false; 55 | 56 | idx=0; 57 | for(int i=2;i<=MX;++i){ 58 | if(prime[i]){ 59 | parr[idx]=i; 60 | pidx[i]=idx; 61 | ++idx; 62 | } 63 | } 64 | 65 | idx=-inf; 66 | for(int i=2;i<=MX;++i){ 67 | if(prime[i]) idx=i; 68 | pprev[i]=idx; 69 | } 70 | 71 | idx=inf; 72 | for(int i=MX;i>0;--i){ 73 | if(prime[i]) idx=i; 74 | pnext[i]=idx; 75 | } 76 | 77 | ll q,l,r; in(q); while(q--){ 78 | in(l); 79 | in(r); 80 | 81 | l=pnext[l]; 82 | r=pprev[r]; 83 | 84 | if(l>=r){ 85 | pc('-'); 86 | out(1); 87 | pc('\n'); 88 | } 89 | else{ 90 | idx=inf; 91 | for(int i=pidx[l];i 3 | using namespace std; 4 | const int MX = 1e6; 5 | const int BITS = 20; 6 | typedef long long ll; 7 | 8 | ll arr[MX+1][BITS]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 12 | int n; cin>>n; for(int i=1;i<=n;++i){ 13 | int x; cin>>x; for(int j=0;j<20;++j) arr[i][j]=arr[i-1][j]+(1&(x>>j)); 14 | } 15 | int q; cin>>q; while(q--){ 16 | ll res=0; int x1,y1,x2,y2,xtot,ytot; cin>>x1>>y1>>x2>>y2; xtot=x2-x1+1; ytot=y2-y1+1; 17 | for(int j=0;j<20;++j) res+=(1L< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | int n=0,m=0; string str; 9 | cin>>str; for(auto s:str) n|=(1<<(s-'a')); 10 | cin>>str; for(auto s:str) m|=(1<<(s-'a')); 11 | (n&m)?cout<<"Yes\n":cout<<"No\n"; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/aliAndHelpingInnocentPeople_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string a; cin>>a; 7 | if(a[2]=='A' or a[2]=='E' or a[2]=='I' or a[2]=='O' or a[2]=='U' or a[2]=='Y'){ cout<<"invalid\n"; return 0; } 8 | if((a[0]+a[1])%2!=0 or (a[3]+a[4])%2!=0 or (a[4]+a[5])%2!=0 or (a[7]+a[8])%2!=0){ cout<<"invalid\n"; return 0; } 9 | else cout<<"valid\n"; return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/aman&Mr.Sharma_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t,res=0; cin>>t; while(t--){ int r,x; cin>>r>>x; if(x*100.0>=(44.0/7.0)*r) res++; } 7 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; while(t--){ 7 | int res=0; unordered_map mppa,mppb; string a,b; cin>>a>>b; 8 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e5; 6 | 7 | ll nn[MX+1],arr[MX+1]; 8 | 9 | int main(){ 10 | for(ll i=1;((i*(i+1))>>1)<=MX;++i) nn[((i*(i+1))>>1)]=((i*(i+1))>>1); 11 | for(ll i=1,tmp=0;i<=MX;++i){ 12 | if(nn[i]==0) nn[i]=tmp; 13 | else tmp=nn[i]; 14 | } 15 | 16 | ll n,mx=-1e9; cin>>n; 17 | for(ll i=1;i<=n;++i){ cin>>arr[i]; arr[i]+=arr[i-1]; }\ 18 | for(ll i=0;imx?tmp:mx; } 19 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,res; cin>>n; 7 | res=(n%10)+2*((n%100)/10)+3*((n%1000)/100)+4*((n%10000)/1000)+5*((n%100000)/10000)+6*((n%1000000)/100000)+7*((n%10000000)/1000000)+8*((n%100000000)/10000000)+9*((n%1000000000)/100000000)+10*((n%10000000000)/1000000000); 8 | res%11==0?cout<<"Legal ISBN\n":cout<<"Illegal ISBN\n"; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/bricksGame_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,res=0; cin>>n; 7 | for(int i=1;;++i){ 8 | res+=i; if(res>n){ cout<<"Patlu\n"; break;} 9 | res+=(i<<1); if(res>n){ cout<<"Motu\n"; break; } 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/charSum_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int res=0; string str; cin>>str; for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; string str; cin>>str>>n; 7 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; while(t--){ int x; cin>>x; cout<<"YES"< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 8 | int t; cin>>t; 9 | while(t--){ 10 | int x=0, y=0, xx, yy, tt; cin>>xx>>yy>>tt; 11 | while(tt--){ int t1,t2; cin>>t1>>t2; x+=t1; y+=t2; } 12 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int l,r,k,res=0; cin>>l>>r>>k; 7 | for(int i=l;i<=r;++i) if(i%k==0) res++; 8 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; while(t--){ 7 | int sh,sm,eh,em; cin>>sh>>sm>>eh>>em; 8 | int tot=(60*eh)-(60*sh)+em-sm; 9 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | string str; cin>>str; 7 | int x=0,y=0; 8 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,res=1; cin>>n; for(int i=1;i<=n;++i) res*=i; 7 | cout< 3 | using namespace std; 4 | const long long MOD = 1e9+7; 5 | 6 | int main(){ 7 | long long res=1; int n; cin>>n; 8 | while(n--){ int x; cin>>x; res=(res*x)%MOD; } 9 | cout< 3 | using namespace std; 4 | 5 | int N; 6 | 7 | void pS(int n){ while(n--) cout<<'*'; } 8 | void pH(int n){ while(n--) cout<<'#'; } 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 12 | int t; cin>>t; while(t--){ 13 | cin>>N; for(int i=1;i<=N;++i){ pS(i); pH((N<<1)-(i<<1)); pS(i); cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int t,n; cin>>t>>n; while(t--){ int x; cin>>x; x>=n?cout<<"YES\n":cout<<"NO\n"; } 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/hello_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | int main(){ cout<<"Hello Kirti"; return 0; } 5 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/ladderophilia_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; cin>>n; cout<<"* *\n* *\n"; 7 | while(n--){ cout<<"*****\n* *\n* *\n"; } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/lifeTheUniverseAndEverything_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | while(1){ 7 | int x; cin>>x; if(x==42) break; 8 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int t,a=0,b=7; cin>>t; 7 | while(t--){ 8 | int x; cin>>x; 9 | if(abs(b-x) 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; while(t--){ 7 | int n; string str; cin>>n>>str; 8 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | int j=1,n,k; cin>>n>>k; 8 | ll res=0, arr[n+1]; 9 | for(ll i=0;i>arr[i]; 10 | for(int i=0;i0){ 13 | for(;j<=k and i+j<=n;++j){ 14 | if(arr[i+j]<0) res+=arr[i+j]; 15 | else break; 16 | } 17 | } 18 | } 19 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | string str; cin>>str; 7 | string strn(str); 8 | reverse(str.begin(),str.end()); 9 | if(str==strn) cout<<"YES"; 10 | else cout<<"NO"; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/playWithNumbers_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e6; 6 | 7 | ll arr[MX]; 8 | 9 | int main(){ 10 | ll n,q,sum=0; cin>>n>>q; 11 | for(int i=1;i<=n;++i){ int x; cin>>x; sum+=x; arr[i]=sum; } 12 | while(q--){ 13 | int x,y; cin>>x>>y; cout<<((arr[y]-arr[x-1])/(y-x+1))<<"\n"; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of IO/primeNumber_VE.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX=1000; 5 | 6 | bool prime[MX]; 7 | 8 | int main(){ 9 | memset(prime,true,sizeof(prime)); 10 | for(int i=2;i*i<=MX;++i) if(prime[i]) for(int j=i*i;j<=MX;j+=i) prime[j]=false; 11 | int n; cin>>n; for(int i=2;i<=n;i++) if(prime[i]) cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,t; cin>>n>>t; 7 | while(t--){ 8 | int h,w; cin>>h>>w; 9 | if(h 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; while(t--){ 7 | int n; cin>>n; 8 | switch(n%12){ 9 | case 0:cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | int t; cin>>t; while(t--){ 8 | int res=0; 9 | string str; cin>>str; 10 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; string str; cin>>n>>str; 7 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | string str; cin>>str; 7 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; cin>>t; 7 | while(t--){ 8 | string a,b; cin>>a>>b; 9 | sort(a.begin(),a.end()); 10 | sort(b.begin(),b.end()); 11 | a==b?cout<<"YES\n":cout<<"NO\n"; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Implementation/anEquilateralTriangle_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | inline ll sumn(int n){return ((n*(n+1)*(n+2))/6);} 7 | 8 | int main(){ 9 | ll res = 0; int num; cin>>num; 10 | while(num>2){ res+=sumn(num-2), num-=2; } 11 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e5; 6 | 7 | int arr[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | int n,q; cin>>n>>q; 12 | for(int i=0;i>arr[i]; 13 | while(q--){ 14 | int x; cin>>x; 15 | if(x==1){ int idx; cin>>idx; cin>>arr[idx]; } 16 | else{ int l,r; cin>>l>>r; cout< 3 | using namespace std; 4 | 5 | char arr[5][5]; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | int t; cin>>t; while(t--){ 10 | for(int i=1;i<=4;++i){ 11 | for(int j=1;j<=4;++j){ 12 | cin>>arr[i][j]; 13 | } 14 | } 15 | bool flg=false; 16 | for(int i=1;i<3 and flg==false;++i){ 17 | for(int j=1;j<3 and flg==false;++j){ 18 | if(arr[i][j]=='.' and arr[i+1][j]=='x' and arr[i+2][j]=='x') flg=true; 19 | if(arr[i][j]=='x' and arr[i+1][j]=='.' and arr[i+2][j]=='x') flg=true; 20 | if(arr[i][j]=='x' and arr[i+1][j]=='x' and arr[i+2][j]=='.') flg=true; 21 | 22 | if(arr[i][j]=='.' and arr[i][j+1]=='x' and arr[i][j+2]=='x') flg=true; 23 | if(arr[i][j]=='x' and arr[i][j+1]=='.' and arr[i][j+2]=='x') flg=true; 24 | if(arr[i][j]=='x' and arr[i][j+1]=='x' and arr[i][j+2]=='.') flg=true; 25 | 26 | if(arr[i][j]=='.' and arr[i+1][j+1]=='x' and arr[i+2][j+2]=='x') flg=true; 27 | if(arr[i][j]=='x' and arr[i+1][j+1]=='.' and arr[i+2][j+2]=='x') flg=true; 28 | if(arr[i][j]=='x' and arr[i+1][j+1]=='x' and arr[i+2][j+2]=='.') flg=true; 29 | } 30 | } 31 | for(int i=1;i<3 and flg==false;++i){ 32 | for(int j=3;j<5 and flg==false;++j){ 33 | if(arr[i][j]=='.' and arr[i+1][j-1]=='x' and arr[i+2][j-2]=='x') flg=true; 34 | if(arr[i][j]=='x' and arr[i+1][j-1]=='.' and arr[i+2][j-2]=='x') flg=true; 35 | if(arr[i][j]=='x' and arr[i+1][j-1]=='x' and arr[i+2][j-2]=='.') flg=true; 36 | 37 | if(arr[i][j]=='.' and arr[i+1][j]=='x' and arr[i+2][j]=='x') flg=true; 38 | if(arr[i][j]=='x' and arr[i+1][j]=='.' and arr[i+2][j]=='x') flg=true; 39 | if(arr[i][j]=='x' and arr[i+1][j]=='x' and arr[i+2][j]=='.') flg=true; 40 | } 41 | } 42 | for(int i=3;i<5 and flg==false;++i){ 43 | for(int j=1;j<3 and flg==false;++j){ 44 | if(arr[i][j]=='.' and arr[i][j+1]=='x' and arr[i][j+2]=='x') flg=true; 45 | if(arr[i][j]=='x' and arr[i][j+1]=='.' and arr[i][j+2]=='x') flg=true; 46 | if(arr[i][j]=='x' and arr[i][j+1]=='x' and arr[i][j+2]=='.') flg=true; 47 | } 48 | } 49 | flg?cout<<"YES\n":cout<<"NO\n"; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Implementation/capitalsAndCities_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | typedef long long ll; 4 | using namespace std; 5 | const int MX = 3e6; 6 | #define f first 7 | #define s second 8 | template void ckmin(T1 &a, T2 b) { a = min(a, b); } 9 | 10 | multiset> arr; 11 | int t,k,cnt,prevs,mn; 12 | ll idx[MX]; 13 | 14 | 15 | long long modMultiply(long long a, long long b){ 16 | long long res = 0; 17 | while(b){ 18 | if(b & 1) res += a; 19 | a *= 2, b>>=1; 20 | } 21 | return res; 22 | } 23 | 24 | void relu(ll& a){ 25 | a=a-k; 26 | if(a<0) a = abs(a)&1?1:0; 27 | } 28 | 29 | //Old solution hence the sub standard implementation 30 | int main(){ 31 | cin>>t>>k; 32 | unsigned long long sum=0; 33 | for(int i=0;i>tmp; arr.emplace(make_pair(tmp,i+1)); sum+=tmp; } 34 | 35 | idx[0]=sum-modMultiply(t,(*arr.begin()).f); prevs=(*arr.begin()).f; 36 | for(auto it:arr){ 37 | if(cnt==0){ cnt++; continue; } 38 | int dif = it.f - prevs; prevs = it.f; 39 | idx[cnt] = idx[cnt-1]+modMultiply(cnt,dif)-modMultiply((t-cnt),dif); 40 | cnt++; 41 | } 42 | 43 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e3; 6 | 7 | pair arr[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | for(int i=0;i<=MX;++i) arr[0].first=0, arr[0].second=0;; 12 | 13 | int s,x,n; cin>>s>>x>>n; 14 | for(int i=1;i<=n;++i) cin>>arr[i].first>>arr[i].second; 15 | sort(arr+1,arr+n+1); 16 | 17 | ll days=0,i=1; 18 | while(s>0){ 19 | ll die = arr[i].first-arr[i-1].first-1; 20 | ll diew = x*die; 21 | 22 | if(diew>s){ days+=s/x+(s%x>0); break; } 23 | else{ days+=die, s-=diew; } 24 | 25 | days++; 26 | if(arr[i].second>s) break; 27 | else s-=arr[i].second; 28 | 29 | if(++i>n){ 30 | days+=s/x+(s%x>0); 31 | break; 32 | } 33 | } 34 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | unordered_map mpp; 7 | 8 | int main(){ 9 | string str; cin>>str; 10 | for(int i=0;i 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX+1]; 7 | 8 | int main() { 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int n, k; cin>>n>>k; for(int i=0;i>arr[i]; 11 | cout<<*max_element(arr+(n-k-1)/2,arr+(n-(n-k)/2)); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Implementation/digitProblem_VE.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | string x; int k; cin>>x>>k; 8 | for(int i=0;i 3 | using namespace std; 4 | 5 | int str[26]; 6 | char iter[] = {'a','c','e','h','k','r','t'}; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int mx=1e9,n; cin>>n; 11 | for(int i=0;i>x; str[x-'a']+=(x=='c' or x=='k' or x=='t')?2:1; } 12 | for(int i=0;i<7;++i) mx=min(str[iter[i]-'a'],mx); 13 | cout<<(mx>>1); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Implementation/theGreatKian_VE.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | typedef long long ll; 4 | using namespace std; 5 | const int MX = 1e5; 6 | 7 | int arr[MX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | int n; cin>>n; 12 | for(int i=0;i>arr[i]; 13 | 14 | for(int i=0;i<3;++i){ 15 | ll res=0; 16 | for(int j=i;j 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ int n,m; cin>>n>>m; cout<<(m%n?"No\n":"Yes\n"); } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Basics of Operators/letUsUnderstandComputer_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const ll MX = 1e6; 6 | 7 | ll arr[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | 12 | for(ll i=1,r=2;i<=MX;i++){ if(i>=r) r<<=1; arr[i]=(i*r)-1; } 13 | int t; cin>>t; while(t--){ ll x; cin>>x; cout< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 10 | int t; cin>>t; while(t--){ 11 | int xr=0,mx,n; cin>>n; 12 | for(int i=0;i>arr[i]; xr^=arr[i]; } 13 | mx=xr; 14 | for(int i=0,x=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | string x,y; while(cin>>x>>y){ 8 | string res; 9 | reverse(x.begin(),x.end()); reverse(y.begin(),y.end()); 10 | x.resize(100,'0'); y.resize(100,'0'); 11 | int carry=0; 12 | for(int i=0;i<100;++i){ 13 | int temp = (x[i]-'0')+(y[i]-'0')+carry; 14 | carry = temp/10; 15 | res.push_back((temp%10)+'0'); 16 | } 17 | reverse(res.begin(),res.end()); 18 | res.erase(0, min(res.find_first_not_of('0'), res.size()-1)); 19 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | unordered_map mpp; 7 | 8 | int main(){ 9 | mpp['a']=mpp['e']=mpp['i']=mpp['o']=mpp['u']=mpp['A']=mpp['E']=mpp['I']=mpp['O']=mpp['U']=true; 10 | int t; cin>>t; while(t--){ 11 | ll res=0,sz; string str; cin>>str; sz=str.size(); for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | const ll MOD = 1e9+7; 6 | 7 | ll xx,yy,tmp; 8 | 9 | ll power(ll x,ll y){ 10 | ll res=1; 11 | while(y>0){ 12 | if(y%2==0){ y/=2; x=(x*x)%MOD; } 13 | else { y-=1; res=(res*x)%MOD; y/=2; x=(x*x)%MOD; } 14 | } 15 | return res; 16 | } 17 | 18 | ll calculateGP(ll firstTerm, ll numberOfTerms, ll len){ 19 | ll commonRatio = power(2,len); 20 | ll summationGP = ((power(commonRatio,numberOfTerms)-1) * power(commonRatio-1,MOD-2))%MOD; 21 | return (summationGP*firstTerm)%MOD; 22 | } 23 | 24 | void g(ll a, ll b){ 25 | if(a%b) { 26 | g(b,a%b); 27 | tmp = xx; 28 | xx = (calculateGP(xx,a/b,b) * power(2,a%b) + yy)%MOD; 29 | yy = tmp; 30 | } else { 31 | yy = power(2,b-1); xx = power(2,a-1); 32 | } 33 | return; 34 | } 35 | 36 | int main(){ 37 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 38 | int t; cin>>t; while(t--){ int x,y; cin>>x>>y; g(x,y); cout< 3 | using namespace std; 4 | 5 | int n; 6 | bool grid[10][10]; 7 | 8 | bool isSafe(int row, int col){ 9 | int i,j; 10 | for(i=0;i=0 and j>=0;--i,--j) if(grid[i][j]) return false; 12 | for(i=row,j=col;i>=0 and j=n) return true; 18 | for(int col=0;col>n; 31 | if(solveQueen(0)){ 32 | for(int i=0;i 3 | using namespace std; 4 | 5 | int n; 6 | bool grid[10][10]; 7 | 8 | bool isSafe(int row, int col){ 9 | int i,j; 10 | for(i=0;i=0 and j>=0;--i,--j) if(grid[i][j]) return false; 12 | for(i=row,j=col;i>=0 and j=n) return true; 18 | for(int col=0;col>n; 31 | if(solveQueen(0)){ 32 | cout<<"YES\n"; 33 | for(int i=0;i 3 | using namespace std; 4 | 5 | int n, res, xM[]={1,2,2,1,-1,-2,-2,-1}, yM[]={-2,-1,1,2,2,1,-1,-2}; 6 | bool grid[10][10]; 7 | 8 | bool isSafe(int row, int col){ return (row<10 and col<10 and row>=0 and col>=0); } 9 | 10 | void solveKnight(int i, int j, int n){ 11 | if(n==0){ if(grid[i][j]==0) grid[i][j]=1, res++; return; } 12 | for(int idx=0;idx<8;++idx){ 13 | if(isSafe(i+xM[idx],j+yM[idx])){ 14 | solveKnight(i+xM[idx],j+yM[idx],n-1); 15 | } 16 | } 17 | } 18 | 19 | int main(){ 20 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 21 | int i,j; cin>>i>>j>>n; solveKnight(--i,--j,n); 22 | cout< 3 | using namespace std; 4 | const int MX = 1000; 5 | 6 | int n; 7 | char forest[MX][MX]; 8 | 9 | int solveForests(int x,int y){ 10 | if(x=0 and y>=0 and forest[x][y]=='T'){ 11 | forest[x][y]='W'; 12 | return solveForests(x-1,y)+solveForests(x,y-1)+solveForests(x+1,y)+solveForests(x,y+1)+1; 13 | } 14 | else return 0; 15 | } 16 | 17 | int main(){ 18 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 19 | int mx=0; cin>>n; 20 | for(int i=0;i>forest[i][j]; 21 | for(int i=0;i 3 | using namespace std; 4 | 5 | int a,b,c,x; 6 | 7 | int minSteps(int d){ return (d>>1)+(d%2); } 8 | 9 | int solveConvert(int b){ 10 | if(a==b) return 0; 11 | if(a>b) return minSteps(a-b); 12 | if(b%c) return solveConvert(b+(c-b%c))+minSteps(c-b%c); 13 | else return solveConvert(b/c)+1; 14 | } 15 | 16 | int main(){ 17 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 18 | int t; cin>>t; while(t--){ cin>>a>>b>>c; cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 7 | int t; cin>>t; while(t--){ 8 | long long n; cin>>n; 9 | if(n&1 or n<4) cout<<"-1\n"; 10 | else if(!(n%4)) cout<<((n>>2)*(n>>2)*(n>>2)*(n>>2))<<'\n'; 11 | else if(!(n%6)) cout<<((n/6)*(n/6)*(n/3)*(n/3))<<'\n'; 12 | else if(!(n%10)) cout<<((n/10)*(n/5)*(n/5)*(n>>1))<<'\n'; 13 | else cout<<"-1\n"; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Recursion and Backtracking/itsConfidential_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | string solveEncrypt(int n,string str){ 6 | if(n<3) return str; 7 | int mid = ((n+(n%2))>>1)-1; 8 | return str.substr(mid,1)+solveEncrypt(mid,str.substr(0,mid))+solveEncrypt(n-mid-1,str.substr(mid+1)); 9 | } 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 13 | int t; cin>>t; while(t--){ 14 | string str; int n; cin>>n>>str; 15 | cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | typedef vector vb; 6 | typedef vector vvb; 7 | 8 | int arr[40],n,m; 9 | vector factors[MX+1]; 10 | 11 | void acceptArray(int n){ for(int i=0;i>arr[i]; } 12 | 13 | void printMat(vvb const &matrix){ 14 | for(auto row: matrix){ for(auto val:row) cout<>1); 28 | ans=mulMat(ans,ans); 29 | if(p%2) ans=mulMat(mat,ans); 30 | return ans; 31 | } 32 | 33 | bool solveMovement(){ 34 | vvb mat(n,vb(n,0)); 35 | for(int i=0;i=0) mat[i][i-f]=1; 38 | } 39 | mat = powerMat(mat,m); 40 | return mat[0][n-1]; 41 | } 42 | 43 | int main(){ 44 | int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; 45 | for(int i:primes) for(int j=i;j<=MX;j+=i) factors[j].emplace_back(i); 46 | 47 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 48 | int t; cin>>t; while(t--){ 49 | cin>>n; acceptArray(n); cin>>m; cout<<(solveMovement()?"YES\n":"NO\n"); 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /BASIC PROGRAMMING/Recursion and Backtracking/simranAndStairs_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int solveHops(int cur){ 6 | if(cur<2) return 1; 7 | else if(cur==2) return 2; 8 | else return solveHops(cur-3)+solveHops(cur-2)+solveHops(cur-1); 9 | //return cur<2?1:cur==2?2:solveHops(cur-3)+solveHops(cur-2)+solveHops(cur-1); //OR USE THIS 10 | } 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 14 | int n; cin>>n; cout< 3 | using namespace std; 4 | 5 | multiset a,b; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int n; cin>>n; 10 | for(int i=0;i>x; a.insert(x); } 11 | for(int i=0;i>x; b.insert(x); } 12 | int xa=a.count(-1), xb=b.count(-1),x=xa+xb; 13 | if(x==1){ 14 | if(xa) if(accumulate(a.begin(),a.end(),1)>accumulate(b.begin(),b.end(),0)){ cout<<'0'; return 0; } 15 | if(xb) if(accumulate(b.begin(),b.end(),1)>accumulate(a.begin(),a.end(),0)){ cout<<'0'; return 0; } 16 | cout<<'1'; return 0; 17 | } 18 | if(x==2){ 19 | if(xa and xb){ cout<<"Infinite\n"s; return 0; } 20 | if(xa){ cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 5000; 6 | 7 | int a[MX+1],b[MX+1],c[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | int n; ll res=0; cin>>n; 12 | for(int i=0;i>a[i]; 13 | for(int i=0;i>b[i]; 14 | for(int i=0;i 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | int arr[MX+1],arrSum[MX+1],arrRes[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n; cin>>n; 11 | for(int i=1;i<=n;++i) cin>>arr[i]; 12 | for(int i=1;i<=n;++i) arrSum[i]=arr[i]+arrSum[i-1]; 13 | for(int i=1;i>q; while(q--){ int l,r; cin>>l>>r; cout<<(arrRes[r]-arrRes[l-1])<<'\n'; } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/R-r-riddikulus!OnceAgain_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | vector vec; int n,d; cin>>n>>d; 8 | while(n--){ int x; cin>>x; vec.emplace_back(x); } 9 | rotate(vec.begin(),vec.begin()+d,vec.end()); 10 | for(auto v:vec) cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | int arr[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int t; cin>>t; while(t--){ 11 | int n; cin>>n; memset(arr,0,sizeof(arr)); 12 | for(int i=0;i>x; arr[x]++; } 13 | for(int i=0;i 3 | using namespace std; 4 | 5 | int arr[]={20,30,40,90,80,270,160,810,320,2430,640,7290,1280,21870,2560,65610,5120,196830,10240,590490}; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int n,mx=0; cin>>n; for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 8 | int n,m; ll res=0; cin>>n>>m; vector nn,mm; 9 | for(int i=0;i>x; nn.emplace_back(x); } 10 | for(int i=0;i>x; mm.emplace_back(x); } 11 | sort(nn.begin(),nn.end()); sort(mm.begin(),mm.end()); 12 | auto i=nn.begin(); auto j=mm.rbegin(); 13 | for(;i!=nn.end() and j!=mm.rend();++i,++j){ 14 | if((*i)<(*j)) res+=((*j)-(*i)); 15 | else break; 16 | } 17 | cout< 3 | using namespace std; 4 | 5 | stack stk; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int n,res=0; cin>>n; 10 | while(n--){ 11 | int x; cin>>x; 12 | if(x==0 and !stk.empty()) stk.pop(); 13 | else stk.push(x); 14 | } 15 | while(!stk.empty()){ res+=stk.top(); stk.pop(); } 16 | cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | int arr[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n,q,t,l,r; cin>>n>>q; for(int i=1;i<=n;++i) cin>>arr[i]; 11 | while(q--){ 12 | cin>>t; 13 | if(t){ cin>>l; arr[l]^=1; } 14 | else { cin>>l>>r; cout<<(arr[min(r,n)]?"ODD":"EVEN")<<'\n'; } 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/canYouSolveIt_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int inf = 1e9; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 8 | int t; cin>>t; while(t--){ 9 | int n,mn1=inf,mx1=-inf,mn2=inf,mx2=-inf; cin>>n; 10 | for(int i=0;i>x; 12 | if(x+i>mx1) mx1=x+i; 13 | if(x+imx2) mx2=x-i; 15 | if(x-i 3 | using namespace std; 4 | const int MX = 1e5; 5 | const int MOD = 1e9+7; 6 | typedef long long ll; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int t; cin>>t; while(t--){ 11 | ll n; cin>>n; 12 | ll arr[n]; 13 | for(int i=0;i>arr[i]; 14 | if(n>=63) cout<<"0\n"; 15 | else{ 16 | ll powerset=((1L<>1),res=0; 17 | for(auto a:arr) if(a>=powerset) res=((res%MOD)+(a%MOD))%MOD; 18 | cout< 3 | using namespace std; 4 | 5 | vector> vec; 6 | 7 | bool pairReverse(const pair &a, const pair &b){ return (a.first>b.first); } 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | int n; cin>>n; while(n--){ 12 | int x; string str; cin>>str>>x; vec.emplace_back(x,str); 13 | } 14 | sort(vec.begin(),vec.end(),pairReverse); 15 | for(int i=0;i<3;++i) cout< 3 | using namespace std; 4 | 5 | int res[10]; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int n; cin>>n; for(int i=1;i<=n;++i){ 10 | int arr[10]={0},x; cin>>x; 11 | for(;x;x/=10) arr[x%10]=1; 12 | for(int j=0;j<10;++j) res[j]+=arr[j]; 13 | } 14 | cout<<*max_element(res,res+10); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/findingTheSubarrays_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 2000; 5 | 6 | double arr[MX+2]; 7 | vector> vec; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | int n; cin>>n; 12 | for(int i=1;i<=n;++i){ int x; cin>>x; arr[i]=arr[i-1]+x; } arr[n+1]=arr[n]; 13 | for(int i=1;i<=n;++i){ 14 | for(int j=i;j<=n;++j){ 15 | if(((arr[j]-arr[i-1])/(j-i+1))>((arr[i-1]+(arr[n+1]-arr[j]))/((i-1+n-j)?(i-1+n-j):1))) vec.emplace_back(make_pair(i,j)); 16 | } 17 | } 18 | cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | int arr[MX+1],arrMx[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n; cin>>n; 11 | for(int i=0;i>arr[i]; 12 | for(int i=n-1;i>=0;--i) arrMx[i]=max(arr[i],arrMx[i+1]); 13 | for(int i=0;i=arrMx[i+1]) cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | string str; cin>>str; sort(str.begin(),str.end()); 9 | int offset=str[0]-'0',n=str.size(); 10 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | int main(){ 8 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 9 | int t; cin>>t; while(t--){ 10 | int n; cin>>n; ll arr[n+1]={0},arrSum[n+1]={0}; 11 | for(int i=1;i<=n;++i){ cin>>arr[i]; arrSum[i]=arrSum[i-1]+arr[i]; } 12 | ll sum = arrSum[n]; 13 | 14 | int q; cin>>q; ll ql[q+1]={0},qr[q+1]={0}; 15 | for(int i=1;i<=q;++i) cin>>ql[i]; 16 | for(int i=1;i<=q;++i) cin>>qr[i]; 17 | for(int i=1;i<=q;++i){ 18 | ll res=0, l=ql[i], r=qr[i], test=(((r-1)/n)-((l-1)/n)); 19 | r=((r-1)%n)+1; 20 | l=((l-1)%n); 21 | res+=((sum%MOD)*(test%MOD))%MOD; 22 | res+=((arrSum[r]-arrSum[l])%MOD+MOD)%MOD; 23 | if(res<0) res=(res+MOD)%MOD; 24 | cout<<(res%MOD)<<' '; 25 | } 26 | cout<<'\n'; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/longATMQueue_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int n,x,p=0,res=1; cin>>n; 8 | while(n--){ cin>>x; if(x 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n,k,res=0; bool flag=false; cin>>n>>k; while(n--){ 11 | int x; cin>>x; 12 | if(x<=k) res++; 13 | else{ if(flag) break; flag=true; } 14 | } 15 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,r,x,y=0,k=0; cin>>n>>r; while(n--){ 9 | cin>>x; if(y 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int n,st=0,mx=0,mxx=0,res=0; cin>>n; 8 | for(int i=1;i<=n;++i){ 9 | int x; cin>>x; mx+=(x?x:-1); 10 | if(mx<0){ mx=0; st=i; } 11 | if(mxx<=mx){ res=i-st; mxx=mx; } 12 | } 13 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int n,k; cin>>n>>k; 8 | queue que; multiset st; 9 | for(int i=1;i<=n;++i){ 10 | int x; cin>>x; st.insert(x); que.push(x); 11 | if(i>=k){ 12 | cout<<(*st.rbegin())<<' '; 13 | int y = que.front(); que.pop(); 14 | st.erase(st.find(y)); 15 | } 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/memoriseMe!_VE.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 1001; 5 | 6 | int arr[MX],x; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n; cin>>n; while(n--){ cin>>x; arr[x]++; } 11 | int q; cin>>q; while(q--){ cin>>x; arr[x]?cout< 3 | using namespace std; 4 | const int inf = 1e9; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 8 | int t; cin>>t; while(t--){ 9 | int x,mn=inf,n,k; cin>>n>>k; while(n--){ cin>>x; mn=min(mn,x); } 10 | cout<<((mn 3 | using namespace std; 4 | const int MX = 1e4; 5 | 6 | int arr[MX+1],n; 7 | 8 | bool check(){ 9 | for(int i=1;i>n; for(int i=0;i>arr[i]; 16 | cout<<(check()?"YES\n":"NO\n"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/monkAndLuckyMinimum_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | multiset st; 9 | int n; cin>>n; while(n--){ 10 | int x; cin>>x; st.insert(x); 11 | } 12 | int mn = *st.begin(),res=0; 13 | for(auto s:st){ 14 | if(s==mn) res++; 15 | else break; 16 | } 17 | cout<<((res&1)?"Lucky\n":"Unlucky\n"); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/monkAndPowerOfTime_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 100; 5 | 6 | int arr[MX]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n,nn,x,idx=0,cidx=0,zeros=0,res=0; cin>>n; nn=n; 11 | for(int i=0;i>arr[i]; 12 | while(nn--){ 13 | cin>>x; for(;;idx++){ 14 | if(!arr[idx%n]) zeros++; 15 | if(arr[idx%n]==x){ 16 | res+=(((idx-cidx)%n)+1)-zeros; 17 | zeros=arr[idx%n]=0; 18 | cidx=idx; 19 | break; 20 | } 21 | } 22 | } 23 | cout< 3 | using namespace std; 4 | #define vi vector 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 8 | int t; cin>>t; while(t--){ 9 | vi vec; int n,k; cin>>n>>k; k%=n; while(n--){ int x; cin>>x; vec.emplace_back(x); } 10 | rotate(vec.rbegin(),vec.rbegin()+k,vec.rend()); 11 | for(auto v:vec) cout< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 10 | int n; cin>>n; 11 | for(int i=0;i>arr[i]; 12 | for(int i=0;i>x; cout<<(arr[i]+x)<<' '; } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/neutralisationOfCharges_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | void stackReversePrint(stack& stk){ 6 | if(stk.empty()) return; 7 | char x=stk.top(); 8 | stk.pop(); 9 | stackReversePrint(stk); 10 | stk.push(x); 11 | cout< stk; int n; cin>>n; while(n--){ 17 | char x; cin>>x; 18 | if(!stk.empty()) (x==stk.top())?stk.pop():stk.push(x); 19 | else stk.push(x); 20 | } 21 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e6; 6 | 7 | int arr[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | ll n,res=0; cin>>n; while(n--){ 12 | int l,r; cin>>l>>r; arr[l]+=1; arr[r+1]+=-1; 13 | } 14 | for(int i=0;i<=MX;++i) arr[i]+=arr[i-1]; 15 | for(int i=0;i<=MX;++i) if(arr[i]==0) res+=i; 16 | cout< 3 | using namespace std; 4 | const int MX = 1e5; 5 | 6 | int arr[MX+1]; 7 | bool prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | memset(prime,true,sizeof(prime)); prime[0]=prime[1]=false; 12 | for(int i=2;i*i<=MX;++i) if(prime[i]) for(int j=(i<<1);j<=MX;j+=i) prime[j]=false; 13 | for(int i=1;i<=MX;++i) arr[i]=arr[i-1]+prime[i]; 14 | 15 | int t; cin>>t; while(t--){ 16 | int l,r,p; cin>>l>>r; p=(arr[r]-arr[l-1]); if(l==1) ++l; 17 | cout<<(p*(r-l-p+1))<<'\n'; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/pepperAndContiguousEvenSubarray_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,res=0,cnt=0; cin>>n; while(n--){ 9 | int x; cin>>x; 10 | if(x&1){ res=max(res,cnt); cnt=0; } 11 | else cnt++; 12 | } 13 | res=max(res,cnt); 14 | cout<<(res?res:-1)<<'\n'; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/perfectSubarray_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 5000; 5 | 6 | int arr[MX+1]; 7 | 8 | bool checkSquare(int x){ double xx=sqrt(x); return ((xx-floor(xx))==0); } 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 12 | int n,res=0; cin>>n; 13 | for(int i=1;i<=n;++i){ cin>>arr[i]; arr[i]+=arr[i-1]; } 14 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,mx=0,sum=0; cin>>n; while(n--){ int x; cin>>x; mx=max(mx,x); sum+=x; } 9 | cout<<(((sum-mx)>mx)?"Yes":"No")<<'\n'; 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /DATA STRUCTURES/1-D/printArrayInReverse_TEST.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int n; cin>>n; int arr[n]; while(n--) cin>>arr[n]; 8 | for(auto a:arr) cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,mx=1e9,res=0; cin>>n; while(n--){ 9 | int x; cin>>x; if(x<=mx){ res++; mx=x; } 10 | } 11 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e5; 6 | 7 | int arr[MX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 11 | int t; cin>>t; while(t--){ 12 | ll n,k,mx=0,res=0; cin>>n>>k; 13 | for(int i=0;i>arr[i]; 14 | for(int i=0;i>x; mx=max(x,mx); } mx++; 15 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int t; cin>>t; while(t--){ 8 | int n,p,q,r,res=0; cin>>n>>p>>q>>r; int arr[n+1]={0}; 9 | for(int i=p;i<=n;i+=p) arr[i]++; 10 | for(int i=q;i<=n;i+=q) arr[i]++; 11 | for(int i=r;i<=n;i+=r) arr[i]++; 12 | for(int i=0;i<=n;++i) if(arr[i]==1) res++; 13 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); 7 | int n=9,a1=0,a2=0; for(int i=1;i<=n;++i){ int x; cin>>x; (i&1)?(a1+=x):(a2+=x); } 8 | cout< 3 | #define ll long long int 4 | using namespace std; 5 | const int MOD = 1000000007; 6 | 7 | long long power(long long y, long long x){ 8 | long long res=1; 9 | while(x>0){ 10 | if(x%2==0) { x/=2; y=((y%MOD)*(y%MOD))%MOD; } 11 | else { x-=1; res=((res%MOD)*(y%MOD))%MOD; x/=2; y=((y%MOD)*(y%MOD))%MOD; } 12 | } 13 | return res; 14 | } 15 | 16 | long long modInverse(long a, long m){ 17 | long long g = __gcd(a, m);//GCD(a, m); 18 | if (g != 1) cerr << "calgorithm::modInverse : Inverse doesn't exist"; 19 | else return power(a, m-2); 20 | } 21 | 22 | long long modMultiply(long long a, long long b){ 23 | long long res = 0; a %= MOD; 24 | while(b){ 25 | if(b & 1) res = (res + a) % MOD; 26 | a = (2 * a) % MOD, b>>=1; 27 | } 28 | return res; 29 | } 30 | 31 | int sumArr[100001], A[100001], temp[100001]; 32 | 33 | int main(){ 34 | ios_base::sync_with_stdio(false); 35 | cin.tie(NULL); 36 | int i,t; cin>>t; 37 | while(t--){ 38 | long long res=0; 39 | int n,tmpp; cin>>n; if(n==1) { cout<<"0\n"; continue; } 40 | for(int i=0;i>tmp; temp[i]=tmp%MOD; } 41 | for(int i=0;i 3 | using namespace std; 4 | 5 | int GCD(int A, int B) { 6 | return B == 0? A : GCD(B, A % B); 7 | } 8 | 9 | void printFac(int num){ 10 | if(num > 1){ 11 | for(int i=2;i<=num;++i){ 12 | if(num%i==0) cout<>m; 19 | vector vec, diff; vec.resize(m); diff.resize(m); 20 | 21 | for(int i=0;i>vec[i]; 22 | 23 | diff[m-1]=abs(vec[m-1]-vec[0]); for(int i=0;i3) for(int i=2;i 3 | using namespace std; 4 | 5 | long long mod_base(long long x){ 6 | if(x<9) return x; 7 | else return x%9 + 10*mod_base(x/9); 8 | } 9 | 10 | int main(){ 11 | long long n; 12 | while(cin>>n) { cout << mod_base(n) << "\n"; } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-1/luckyStringJackpot_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | #define ll long long int 4 | using namespace std; 5 | const int MOD = 1000000007; 6 | 7 | vector vec; 8 | 9 | long long power(long long y, long long x){ 10 | long long res=1; 11 | while(x>0){ 12 | if(x%2==0) { x/=2; y*=y; } 13 | else { x-=1; res*=y; x/=2; y*=y; } 14 | } 15 | return res; 16 | } 17 | 18 | void genSeq(){ 19 | vec.emplace_back(0); 20 | long long x = 0,cnt=1; 21 | for(long long i=1;i<45;i++){ 22 | long long tmp = x+i*power(2,i)+cnt; 23 | vec.emplace_back(tmp); 24 | vec.emplace_back(tmp+1); 25 | x=x+i*power(2,i); 26 | cnt+=2; 27 | } 28 | } 29 | int main(){ 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | genSeq(); 33 | int t; cin>>t; 34 | while(t--){ 35 | long long n; cin>>n; 36 | auto lb = lower_bound(vec.begin(),vec.end(),n); 37 | cout< 3 | using namespace std; 4 | const int MX = 1000000; 5 | 6 | int vec[MX+1]; 7 | 8 | long long fac(int x){ 9 | long long res=1; 10 | for(long long i = 1; i <= x; i++) res = i*res; 11 | return res; 12 | } 13 | 14 | void genPanDigit(){ 15 | for(int i=1;i<7;++i){ 16 | string str; int tmp=0; for(int j=0;j<=i;++j){ tmp=10*tmp+j; } 17 | str = to_string(tmp); 18 | for(int j=0;j>t; 30 | while(t--){ int x,y; cin>>x>>y; cout< 3 | using namespace std; 4 | const int MOD = 1000000007; 5 | 6 | bool prime[1000001]; 7 | int sum_prime[1000001]; 8 | 9 | int main(){ 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-1/mystery_VE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; cin>>t; 6 | while(t--){ 7 | int res=0, x; cin>>x; 8 | for(int i=1;i<=sqrt(x);i++){ 9 | if(x%i==0){ 10 | if(x/i == i) {res++; continue;} 11 | res+=2; 12 | } 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | const int MOD = 1000003; 5 | 6 | int main(){ 7 | int t; cin >> t; 8 | vectorvec; vec.resize(MOD+1); vec[0]=1; 9 | for(int i=1;i<=MOD;i++) vec[i] = (vec[i-1]*i)%MOD; 10 | for(int i=0;i>n>>k; 12 | if(n 2 | #define ll long long int 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int t; cin>>t; 9 | while(t--){ 10 | ll x; cin>>x; x = x>1?floor(x*x/2)-1:1; cout< 2 | #define ll long long int 3 | using namespace std; 4 | const int MOD = 1000000007; 5 | 6 | 7 | long long modMultiply(long long a, long long b, long long m = MOD){ 8 | long long res = 0; a %= m; 9 | while(b){ 10 | if(b & 1) res = (res + a) % m; 11 | a = (2 * a) % m, b>>=1; 12 | } 13 | return res; 14 | } 15 | 16 | long long power(long long y, long long x){ 17 | long long res=1; 18 | while(x>0){ 19 | if(x%2==0) { x/=2; y=modMultiply(y,y)%MOD; } 20 | else { x-=1; res=modMultiply(res,y)%MOD; x/=2; y=modMultiply(y,y)%MOD; } 21 | } 22 | return res; 23 | } 24 | 25 | int main(){ 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(NULL); 28 | long long x, y=0; string str; cin>>x>>str; 29 | for(int i=0;i 2 | using namespace std; 3 | const int MOD = 1000000007; 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | int t; cin>>t; 8 | while(t--){ long long x; cin >> x; cout<<((x%MOD)*((2*x-1)%MOD))%MOD<<"\n"; } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-1/royAndBirthday_E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | using namespace std; 4 | const int MOD = 1000000007; 5 | #define maxn 1000009 6 | 7 | long long fact[maxn]; 8 | 9 | void genFactorial(){ 10 | fact[0] = 1; 11 | for(long long i = 1; i < maxn; i++) fact[i] = i*fact[i-1]%MOD; 12 | } 13 | 14 | long long power(long long x, long long y){ 15 | if(y == 0) return 1; 16 | long long p = power(x, y/2) % MOD; p = (p * p) % MOD; 17 | return (y%2 == 0)? p : (x * p) % MOD; 18 | } 19 | 20 | long long modInverse(long a, long m){ 21 | long long g = __gcd(a, m); 22 | if (g != 1) cout << "Inverse doesn't exist"; 23 | else return power(a, m-2); 24 | } 25 | 26 | long long startPosCount(long long c, long long v){ long long res = ((((fact[c]%MOD)*(power(fact[v],MOD-2)%MOD))%MOD)*(power(fact[c-v],MOD-2)%MOD))%MOD; return res; } 27 | 28 | int main(){ 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | genFactorial(); 32 | 33 | int t; string temp_str; cin>>t; getline(cin,temp_str); 34 | while(t--){ 35 | string str; 36 | unordered_map mpp; 37 | getline(cin,str); 38 | int vow=0,coso=0; 39 | 40 | for(int i=0;i=vow-1){ 48 | long long vnum = fact[vow], vden = 1, cnum = fact[coso], cden = 1, res; 49 | for(int i=0;i<26;++i){ 50 | if(mpp[i]>1){ 51 | if(i == 0 || i == 4 || i == 8 || i == 14 || i == 20) vden=((vden%MOD)*(fact[mpp[i]]%MOD))%MOD; 52 | else cden=((cden%MOD)*(fact[mpp[i]]%MOD))%MOD; 53 | } 54 | } 55 | //cout<<"("< 2 | #define ll long long int 3 | using namespace std; 4 | const int MOD = 1000000007; 5 | 6 | ll poww(ll pw){ 7 | ll res=1, base=2; 8 | while(pw>0){ 9 | if(pw%2==0) {pw/=2; base=((base%MOD)*(base%MOD))%MOD;} 10 | else { pw-=1; res=((res%MOD)*(base%MOD))%MOD; pw/=2; base=((base%MOD)*(base%MOD))%MOD;} 11 | } 12 | return res; 13 | } 14 | 15 | int main(){ 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int t; cin>>t; 19 | while(t--){ 20 | ll n; cin>>n; n=poww(n)-1; cout< 2 | using namespace std; 3 | 4 | const int MOD = 1000000007; 5 | 6 | int GCD(int A, int B) { return B == 0? A : GCD(B, A % B); } 7 | 8 | int main(){ 9 | vector vec; 10 | long long res=1,ans=1; 11 | int n,g; cin>>n; 12 | for(int i=0;i>x; vec.emplace_back(x); } 13 | g = vec[0]; 14 | for(auto i:vec) g = GCD(g,i); 15 | for(auto i:vec) res=(res*i)%MOD; 16 | for(int i=0;i 2 | #define ll long long int 3 | using namespace std; 4 | const int MOD = 1000000007; 5 | 6 | long long power(long long x, long long y, long long m){ 7 | if(y == 0) return 1; 8 | long long p = power(x, y/2, m) % m; 9 | p = (p * p) % m; 10 | return (y%2 == 0)? p : (x * p) % m; 11 | } 12 | 13 | long long modInverse(long a, long m){ 14 | long long g = __gcd(a, m); 15 | if (g != 1) cout << "Inverse doesn't exist"; 16 | else return power(a, m-2, m); 17 | } 18 | 19 | int main(){ 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | int t; string temp_str; cin>>t; getline(cin,temp_str); 23 | while(t--){ 24 | unordered_map mpp; 25 | string str; 26 | 27 | getline(cin,str); 28 | //Find freq of each digit in input number 29 | for(int i=0;i1){ 36 | long long num = 1; 37 | for(int j=mpp[i];j>0;j--) num = ((num%MOD)*(j%MOD))%MOD; 38 | den = ((den%MOD)*(num%MOD))%MOD; 39 | } 40 | } 41 | 42 | //Find numerator 43 | long long num = 1; 44 | //Find factorial of the length of the input number and save it as numerator 45 | for(int i=str.size();i>0;i--) num = ((num%MOD)*(i%MOD))%MOD; 46 | 47 | //Final answer numerator * modulo_inverse_of_denominator 48 | long long res = ((num%MOD)*(modInverse(den,MOD)%MOD))%MOD; 49 | cout< 2 | #define ll long long int 3 | using namespace std; 4 | const int MOD = 1000000009; 5 | 6 | long long modMultiply(long long a, long long b){ 7 | long long res = 0; a %= MOD; 8 | while(b){ 9 | if(b & 1) res = (res + a) % MOD; 10 | a = (2 * a) % MOD, b>>=1; 11 | } 12 | return res; 13 | } 14 | 15 | long long factorial(long long n){ 16 | if(n==0) return 1; 17 | long long res=1; 18 | for(int i=1;i<=n;++i) res = modMultiply(res,i); 19 | return res; 20 | } 21 | 22 | long long power(long long y, long long x){ 23 | long long res=1; 24 | while(x>0){ 25 | if(x%2==0) { x/=2; y=((y%MOD)*(y%MOD))%MOD; } 26 | else { x-=1; res=((res%MOD)*(y%MOD))%MOD; x/=2; y=((y%MOD)*(y%MOD))%MOD; } 27 | } 28 | return res; 29 | } 30 | 31 | int main(){ 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | long long n; cin>>n;// cout< 2 | using namespace std; 3 | 4 | const int MOD = 1000000007; 5 | 6 | int main(){ 7 | int t; cin>>t; 8 | while(t--){ long long n; cin>>n; cout<<(((((((n%MOD)*((n-1)%MOD))%MOD)*(n-1)%MOD))%MOD)*(250000002%MOD))%MOD<<" "<<(((((((n%MOD)*((n-1)%MOD))%MOD)*(2*n-1)%MOD))%MOD)*(166666668%MOD))%MOD<<"\n"; } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-1/unlockTheDoor_M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int MOD = 1000000007; 3 | using namespace std; 4 | 5 | long long modMultiply(long long a, long long b){ 6 | long long res = 0; a %= MOD; 7 | while(b){ 8 | if(b & 1) res = (res + a) % MOD; 9 | a = (2 * a) % MOD, b>>=1; 10 | } 11 | return res; 12 | } 13 | 14 | long long power(long long x, long long y){ 15 | long long res=1; 16 | while(y){ 17 | if(y%2==0) { y/=2; x = modMultiply(x,x); } 18 | else { y-=1; res = modMultiply(res,x); y/=2; x=modMultiply(x,x); } 19 | } 20 | return res; 21 | } 22 | 23 | long long factorial(long long n){ 24 | if(n==0) return 1; 25 | long long res=1; 26 | for(int i=1;i<=n;++i) res = modMultiply(res,i); 27 | return res; 28 | } 29 | 30 | int main(){ 31 | int t; cin>>t; 32 | while(t--){ 33 | int n,k; cin>>n>>k; 34 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 50000; 6 | 7 | int arr[MX+1]; 8 | 9 | int main(){ 10 | int t; cin>>t; 11 | while(t--){ 12 | int n,c,res=0; cin>>arr[0]>>arr[1]>>c>>n; 13 | for(int i=2;i 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+9; 6 | 7 | ll power(ll x, ll y){ 8 | ll res=1; x%=MOD; 9 | while(y>0){ 10 | if(y&1) res=(res*x)%MOD; 11 | y>>=1, x=(x*x)%MOD; 12 | } 13 | return res; 14 | } 15 | //Even terms from nC0*9^n to nC1*9^0 = ((9+1)^10 - (9-1)^10)/2 16 | int main(){ 17 | ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 18 | int t; cin>>t; while(t--){ ll n; cin>>n; cout<<(((power(10,n)-power(8,n))+MOD)*500000005)%MOD<<"\n"; } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-2/ashuAndPrimeFactors_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 1000000; 5 | 6 | int minPrime[MX+1],countPrime[MX+1]; 7 | 8 | int main(){ 9 | for(int i = 2; i*i <= MX; ++i) if(!minPrime[i]) for(int j = i*i; j <= MX; j += i) if(!minPrime[j]) minPrime[j]=i; 10 | for(int i = 0; i <= MX; ++i) if(!minPrime[i]) minPrime[i] = i; 11 | for(int i = 0; i <= MX; ++i) countPrime[minPrime[i]]++; 12 | 13 | int t; cin>>t; 14 | while(t--){ int n; cin>>n; cout< 3 | typedef long long ll; 4 | using namespace std; 5 | template void ckmax(T &a, T b) { a = max(a, b); } 6 | 7 | set st; 8 | unordered_map mpp; 9 | /* 10 | n*(n-1)/2 for n=1000000 is 4999950000. 11 | Hence use instead of for mapping even is res is ll 12 | */ 13 | int main(){ 14 | ios_base::sync_with_stdio(0); cin.tie(0); 15 | int t; cin>>t; 16 | while(t--){ 17 | ll res=0; int n, mx=0; cin>>n; st.clear(), mpp.clear(); 18 | while(n--){ int n; cin>>n; st.emplace(n); mpp[n]++; ckmax(mx,n);} 19 | for(auto i:st) for(int j=2*i;j<=mx;j+=i){ res+=mpp[i]*mpp[j]; } 20 | for(auto i:st) { res+=mpp[i]*(mpp[i]-1)/2; } 21 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1000000007; 6 | const int MX = 1000000; 7 | template void ckmax(T1 &a, T2 b) { a = max(a, b); } 8 | 9 | int facCount[MX+1]; 10 | ll proCount[MX+1]; 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 14 | int pos=0; 15 | for(int i=1;i<=MX;++i) for(int j=i;j<=MX;j+=i) facCount[j]++; 16 | for(int i=1;i<=MX;++i,pos++){ 17 | proCount[i]=i*facCount[i]; 18 | ckmax(proCount[i],proCount[i-1]+pos); 19 | if(proCount[i]!=proCount[i-1]) pos=0; 20 | } 21 | int t; cin>>t; 22 | while(t--){ int n; cin>>n; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e6; 6 | 7 | int spf[MX+1]; 8 | bool mk[MX+1]; 9 | 10 | bool check(){ 11 | int x; cin>>x; if(x<4) return false; 12 | ll y=x; vector vec; 13 | 14 | while(x!=1){ int xx=spf[x]; vec.emplace_back(xx); while(x%xx==0) x/=xx; } 15 | 16 | for(int i=0;i>n>>q; 25 | 26 | while(n--){ int x; cin>>x; mk[x]=true; } 27 | for(ll i=2;i<=MX;++i) if(mk[i]) for(ll j=i;j<=MX;j*=i) mk[j]=true; 28 | mk[1]=true; 29 | 30 | while(q--){ check()?cout<<"YES\n":cout<<"NO\n"; } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-2/holiAndDivisionFunction_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const ll MX = 1e5; 6 | 7 | ll arr[MX+1],pre[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | int t; cin>>t; while(t--){ 12 | memset(arr,0,sizeof(arr)); 13 | memset(pre,0,sizeof(pre)); 14 | 15 | ll n,mx=0,ans=0; cin>>n; 16 | for(int i=1;i<=n;++i){ cin>>arr[i]; pre[arr[i]]++; mx=max(mx,arr[i]); } 17 | for(int i=1;i<=MX;++i) pre[i]+=pre[i-1]; 18 | 19 | for(ll i=1;i<=n;++i){ 20 | ll tmp=arr[i],cnt=1; 21 | for(double j=0.5;;++j,++cnt){ 22 | ll x=j*tmp,y=(j+1)*tmp; 23 | if(tmp%2==0) --x, --y; 24 | if(x>mx) break; 25 | ans += cnt*(pre[min(y,mx)]-pre[x]); 26 | } 27 | } 28 | cout< 3 | using namespace std; 4 | const int MX = 2*1e5; 5 | 6 | int spf[MX+1],arr[MX+1],ans[MX+1]; 7 | unordered_map mpp; 8 | 9 | int main(){ 10 | for(int i=2;i<=MX;++i) if(spf[i]==0) for(int j=i;j<=MX;j+=i) if(spf[j]==0) spf[j]=i; 11 | 12 | int n; cin>>n; 13 | for(int i=1;i<=n;++i) cin>>arr[i]; 14 | 15 | for(int i=n;i>0;--i){ 16 | int x = arr[i]; 17 | while(x!=1){ 18 | int y=spf[x]; 19 | if(mpp[y]==0) mpp[y]=i; 20 | else if(ans[i]==0) ans[i]=mpp[y], mpp[y]=i; 21 | else ans[i]=min(ans[i],mpp[y]), mpp[y]=i; 22 | while(x%y==0) x/=y; 23 | } 24 | } 25 | 26 | mpp.clear(); 27 | for(int i=1;i<=n;++i){ 28 | int x = arr[i]; 29 | while(x!=1){ 30 | int y=spf[x]; 31 | if(mpp[y]==0) mpp[y]=i; 32 | else if(ans[i]==0) ans[i]=mpp[y], mpp[y]=i; 33 | else ans[i]=i-mpp[y]>abs(ans[i]-i)?ans[i]:mpp[y], mpp[y]=i; 34 | while(x%y==0) x/=y; 35 | } 36 | } 37 | for(int i=1;i<=n;++i) ans[i]==0?cout<<"-1 ":cout< 2 | using namespace std; 3 | typedef long long ll; 4 | const int MOD = 1000000007; 5 | //Modular logic is correct, but the implementation of DS is horrible and is not recommended. 6 | vector aFac, bFac; 7 | unordered_map aFacP, bFacP; 8 | 9 | ll modMultiply(ll x, ll y){ 10 | ll res=0; x%=MOD; 11 | while(y){ 12 | if(y&1) res=(res+x)%MOD; 13 | x=(x<<1)%MOD, y>>=1; 14 | } 15 | return res; 16 | } 17 | 18 | ll power(ll x, ll y){ 19 | ll res=1; 20 | while(y){ 21 | if(y%2==0) { y>>=1, x=modMultiply(x,x); } 22 | else { res=modMultiply(res,x), y>>=1, x=modMultiply(x,x);} 23 | } 24 | return res; 25 | } 26 | 27 | void findFac(ll a, ll b, ll c){ 28 | aFac.clear(), bFac.clear(), aFacP.clear(), bFacP.clear(); 29 | for(ll i=2;i*i<=a;++i){ 30 | while(a%i==0){ 31 | if(aFacP[i]==0) aFac.emplace_back(i); 32 | aFacP[i]++, a/=i; 33 | } 34 | } 35 | if(a!=1) { aFac.emplace_back(a),aFacP[a]++; } 36 | for(ll i=2;i*i<=b;++i){ 37 | while(b%i==0){ 38 | if(bFacP[i]==0) bFac.emplace_back(i); 39 | bFacP[i]++, b/=i; 40 | } 41 | } 42 | if(b!=1) { bFac.emplace_back(b),bFacP[b]++; } 43 | for(auto i:bFac) bFacP[i] = bFacP[i]*c; 44 | } 45 | 46 | ll negDel(ll a){ 47 | if(a>=0) return a; 48 | return 0; 49 | } 50 | 51 | int main(){ 52 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 53 | int t; cin>>t; 54 | while(t--){ 55 | ll a,b,c,res=1; cin>>a>>b>>c; c%=MOD-1; findFac(a,b,c); 56 | for(auto i:bFac) { bFacP[i] = negDel(bFacP[i]-aFacP[i]); res=(res * power(i,bFacP[i]))%MOD; } 57 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | const int MOD = 1000000007; 5 | 6 | vector vec; 7 | unordered_map mpp; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 11 | int t; cin>>t; 12 | for(int i=0;i>a>>b>>c>>d; 14 | ll x = __gcd(abs(a-c),abs(b-d))+1; 15 | vec.emplace_back(x); 16 | if(mpp[x]==0) mpp[x]=i+1; 17 | } 18 | sort(vec.begin(),vec.end()); 19 | 20 | int q; cin>>q; 21 | while(q--){ 22 | ll n; cin>>n; 23 | auto it = lower_bound(vec.begin(),vec.end(),n); 24 | if(it==vec.end()) { cout<<"-1\n"; continue; } 25 | else cout< 3 | using namespace std; 4 | const int MX = 1000000; 5 | 6 | set st; 7 | int prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); 11 | for(int i = 2; i*i <= MX; ++i) if(!prime[i]) for(int j = i*i; j <= MX; j+=i) if(!prime[j]) prime[j]=1; 12 | for(int i = 2; i <= MX; ++i) if(!prime[i]) st.emplace(i); 13 | 14 | int t; cin>>t; 15 | while(t--){ 16 | int n; cin>>n; auto it = st.lower_bound(n); 17 | if(*(it)-n==0) cout<<*it<<"\n"; 18 | else if(*it-n < n-*(--it)) cout<<*(++it)<<"\n"; 19 | else cout<<*it<<"\n"; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-2/numberOfPrimes_TEST.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n,cnt=0; cin>>n; 7 | bool prime[n+1]; 8 | for(int i=0;i<=n;++i) prime[i]=true; 9 | prime[0]=false; prime[1]=false; 10 | for(int i=2;i*i<=n;++i) if(prime[i]) for(int j=i*i;j<=n;j+=i) prime[j]=false; 11 | for(int i=2;i<=n;++i) if(prime[i]) cnt++; 12 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | const int MOD = 1000000007; 5 | 6 | ll inc(ll m, ll n){ 7 | ll a,b; 8 | a = (n/m) + m, b = (n/m) + 1 - m, a = ((a % MOD) * (b % MOD)), a %= MOD, a *= m, a %= MOD; 9 | return a; 10 | } 11 | 12 | ll sum(ll n){ 13 | ll res; 14 | res = (((n % MOD) * ((n+1) % MOD)) % MOD) * (((2*n) %MOD + 1) % MOD), res %= MOD, res *= 166666668, res %= MOD; 15 | return res; 16 | } 17 | 18 | int main(){ 19 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 20 | ll m,n,res=0; cin>>n; 21 | for(m=1;m*m<=n;++m){ res += inc(m,n), res %= MOD; } 22 | res -= sum(m-1); 23 | if(res<0) res += MOD; 24 | cout< 3 | typedef long long ll; 4 | using namespace std; 5 | const int MX = 1000000; 6 | 7 | bool prime[MX+1]; 8 | ll sumPrime[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); 12 | 13 | 14 | memset(prime,true,sizeof(prime)); 15 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 16 | prime[0] = false ,prime[1] = false, sumPrime[0]=0; 17 | 18 | for(int i=1;i<=MX;++i){ sumPrime[i]+=sumPrime[i-1]; if(prime[i]) sumPrime[i]+=i; } 19 | 20 | int t; cin>>t; 21 | while(t--){ 22 | ll l,r,res=0; cin>>l>>r; 23 | if(l==r){ cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1000003; 6 | const int MX = 2000; 7 | 8 | ll phi[MOD+4],arr[MX+1],summation[MOD+4]; 9 | unsigned long long summ[MOD+4]; 10 | 11 | long long modMultiply(long long a, long long b){ 12 | long long res = 0; a %= MOD; 13 | while(b){ 14 | if(b & 1) res = (res + a) % MOD; 15 | a = (2 * a) % MOD, b>>=1; 16 | } 17 | return res; 18 | } 19 | 20 | long long power(long long x, long long y){ 21 | long long res=1; 22 | while(y>0){ 23 | if(y%2==0) { y/=2; x=modMultiply(x,x); } 24 | else { y-=1; res=modMultiply(res,x); y/=2; x=modMultiply(x,x); } 25 | } 26 | return res; 27 | } 28 | 29 | int main(){ 30 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 31 | 32 | for(ll i=1;i<=MOD+3;++i) phi[i] = i; 33 | for(ll p=2;p<=MOD+3;++p){ 34 | if(phi[p]==p){ 35 | phi[p]=p-1; 36 | for(ll i=2*p;i<=MOD+3;i+=p) phi[i]=(phi[i]/p)*(p-1); 37 | } 38 | } 39 | 40 | summ[0] = 0; 41 | for(ll i = 1; i <= MOD+3; i++) summ[i] = summ[i-1]+i; 42 | 43 | ll modI2 = power(2,MOD-2); 44 | for(ll i=2;i<=MOD+3;++i) summation[i]=(summ[i]-(modMultiply(modMultiply(i,phi[i]),modI2)))%MOD; 45 | summation[1]=1; 46 | 47 | int t; cin>>t; 48 | while(t--){ 49 | int n,q; cin>>n>>q; 50 | for(int i=1;i<=n;++i){ int tmp; cin>>tmp; arr[i]=summation[tmp]; } 51 | while(q--){ 52 | int type; cin>>type; 53 | if(type==1){ int l,r; cin>>l>>r; cout<<*max_element(arr+l,arr+r+1)<<"\n";} 54 | else{ int id; cin>>id; arr[id] = summation[arr[id]]; } 55 | } 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /MATHS/Basic Number Theory-2/zrZr_E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0); cin.tie(0); 7 | int t; cin>>t; 8 | while(t--){ 9 | ll n,res=0; cin>>n; for(ll i = 5; n/i >= 1; i*=5) res += n/i; 10 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e6; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | ll mM(ll a, ll b){ 11 | ll res = 0; a %= MOD; 12 | while(b){ 13 | if(b & 1) res = (res + a) % MOD; 14 | a = (2 * a) % MOD, b>>=1; 15 | } 16 | return res; 17 | } 18 | 19 | ll power(ll x, ll y){ 20 | ll res=1; 21 | while(y>0){ 22 | if(y&1) res=mM(res,x); 23 | y>>=1, x=mM(x,x); 24 | } 25 | return res; 26 | } 27 | 28 | int main(){ 29 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 30 | 31 | fact[0]=1; 32 | for(ll i=1;i<=MX;++i) fact[i]=(fact[i-1]*i)%MOD; 33 | 34 | inv[MX]=power(fact[MX],MOD-2); 35 | for(ll i=MX-1;i>=0;--i) inv[i]=((i+1)%MOD*inv[i+1]%MOD)%MOD; 36 | 37 | ll t; cin>>t; 38 | while(t--){ 39 | ll n,m,k; 40 | cin>>n>>m>>k; 41 | n-=(m-1)*k; 42 | if(n 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e5+1e5-2; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | fact[0] = 1,inv[0]=1,inv[1]=1; 14 | for(ll i=1;i<=MX;++i) fact[i] = (i*fact[i-1])%MOD; //Find factorial 15 | for(ll i=2;i<=MX;++i) inv[i] = (-(MOD/i)*inv[MOD%i])%MOD + MOD;//Find Inverse Modulo of i under MOD 16 | for(ll i=2;i<=MX;++i) inv[i] = (inv[i]*inv[i-1])%MOD;//Find factorial of Inverse Modulo of i under MOD 17 | 18 | int t; cin>>t; while(t--){ 19 | ll res=0,n,m,k; cin>>n>>m>>k; while(k--){ 20 | ll i,j,p; cin>>i>>j>>p; 21 | ll x1 = i+j-2; 22 | ll x2 = i-1; 23 | ll x3 = j-1; 24 | ll t1 = (fact[x1]*((inv[x2]*inv[x3])%MOD))%MOD;//Possible way to reach (i,j) from (1,1) 25 | ll y1 = n-i+m-j; 26 | ll y2 = n-i; 27 | ll y3 = m-j; 28 | ll t2 = (fact[y1]*((inv[y2]*inv[y3])%MOD))%MOD;//Possible way ro reach (n,m) from (i,j) 29 | res = (res + ((p*((t1*t2)%MOD))%MOD))%MOD;//All possible way to reach (n,m) from (1,1) passing from (i,j) multiplied by power 30 | //res = (res + mM(p,mM((fact[i+j-2]*(inv[i-1]*inv[j-1])%MOD)%MOD,(fact[n-i+m-j]*(inv[n-i]*inv[m-j])%MOD)%MOD)))%MOD; //TLE 31 | } 32 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 20; 6 | 7 | ll arr[MX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | ll res=0,t,n; cin>>t>>n; 12 | for(int i=0;i>arr[i]; 13 | for(int i=1;i<(1< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e6; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | ll mM(ll a, ll b){ 11 | ll res = 0; a %= MOD; 12 | while(b){ 13 | if(b & 1) res = (res + a) % MOD; 14 | a = (a<<1) % MOD, b>>=1; 15 | } 16 | return res; 17 | } 18 | 19 | ll power(ll x, ll y){ 20 | ll res=1; 21 | while(y>0){ 22 | if(y&1) res=mM(res,x); 23 | y>>=1, x=mM(x,x); 24 | } 25 | return res; 26 | } 27 | 28 | int main(){ 29 | fact[0]=1;inv[0]=1;inv[1]=1; 30 | for(int i=1;i<=MX;++i) fact[i]=(fact[i-1]*i)%MOD; 31 | for(int i=2;i<=MX;++i) inv[i]=(-(MOD/i)*inv[MOD%i])%MOD+MOD; 32 | for(int i=2;i<=MX;++i) inv[i]=(inv[i]*inv[i-1])%MOD; 33 | 34 | int t; cin>>t; while(t--){ 35 | ll n,k; cin>>n>>k; 36 | ll res = (fact[n]*((inv[n-k]*inv[k])%MOD))%MOD; 37 | res=(res*(power(2,k)%MOD))%MOD; 38 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e5+1e5-2; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | fact[0] = 1,inv[0]=1,inv[1]=1; 14 | for(ll i=1;i<=MX;++i) fact[i] = (i*fact[i-1])%MOD; //Find factorial 15 | for(ll i=2;i<=MX;++i) inv[i] = (-(MOD/i)*inv[MOD%i])%MOD + MOD;//Find Inverse Modulo of i under MOD 16 | for(ll i=2;i<=MX;++i) inv[i] = (inv[i]*inv[i-1])%MOD;//Find factorial of Inverse Modulo of i under MOD 17 | 18 | int t; cin>>t; while(t--){ 19 | ll res=0,n,m,k; cin>>n>>m>>k; 20 | while(k--){ 21 | ll i,j,p; cin>>i>>j>>p; 22 | ll x1 = i+j-2; 23 | ll x2 = i-1; 24 | ll x3 = j-1; 25 | ll t1 = (fact[x1]*((inv[x2]*inv[x3])%MOD))%MOD;//Possible way to reach (i,j) from (1,1) 26 | ll y1 = n-i+m-j; 27 | ll y2 = n-i; 28 | ll y3 = m-j; 29 | ll t2 = (fact[y1]*((inv[y2]*inv[y3])%MOD))%MOD;//Possible way ro reach (n,m) from (i,j) 30 | res = (res + ((p*((t1*t2)%MOD))%MOD))%MOD;//All possible way to reach (n,m) from (1,1) passing from (i,j) multiplied by power 31 | //res = (res + mM(p,mM((fact[i+j-2]*(inv[i-1]*inv[j-1])%MOD)%MOD,(fact[n-i+m-j]*(inv[n-i]*inv[m-j])%MOD)%MOD)))%MOD; //TLE 32 | } 33 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 100; 6 | 7 | ll fact[MX+1]; 8 | 9 | ll mM(ll a, ll b){ 10 | ll res = 0; 11 | while(b){ 12 | if(b & 1) res += a; 13 | a *= 2, b>>=1; 14 | } 15 | return res; 16 | } 17 | 18 | ll power(ll x, ll y){ 19 | ll res=1; 20 | while(y>0){ 21 | if(y&1) res=mM(res,x); 22 | y>>=1, x=mM(x,x); 23 | } 24 | return res; 25 | } 26 | 27 | int main(){ 28 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 29 | 30 | fact[0]=1; 31 | for(ll i=1;i<=MX;++i) fact[i]=(fact[i-1]*i); 32 | 33 | int n,k,m=0; 34 | cin>>n>>k; 35 | while(n--){ int x; cin>>x; if(x%2==0) m++; } 36 | m=(m>=k)?fact[m]/(fact[m-k]*fact[k]):0; 37 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | 7 | ll modMultiply(ll a, ll b){ 8 | ll res = 0; a %= MOD; 9 | while(b){ 10 | if(b & 1) res = (res + a) % MOD; 11 | a = (a<<1) % MOD, b>>=1; 12 | } 13 | return res; 14 | } 15 | 16 | ll power(ll x, ll y){ 17 | ll res=1; 18 | while(y>0){ 19 | if(y&1) res=modMultiply(res,x); 20 | y>>=1, x=modMultiply(x,x); 21 | } 22 | return res; 23 | } 24 | 25 | int main(){ 26 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 27 | 28 | int t; cin>>t; while(t--){ 29 | ll res=0,n; cin>>n; 30 | res=(res+power(4,n))%MOD; 31 | res=(res-power(2,n))%MOD; 32 | res=res<0?res+MOD:res; 33 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e5+1e5-2; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | fact[0] = 1,inv[0]=1,inv[1]=1; 14 | for(ll i=1;i<=MX;++i) fact[i] = (i*fact[i-1])%MOD; //Find factorial 15 | for(ll i=2;i<=MX;++i) inv[i] = (-(MOD/i)*inv[MOD%i])%MOD + MOD;//Find Inverse Modulo of i under MOD 16 | for(ll i=2;i<=MX;++i) inv[i] = (inv[i]*inv[i-1])%MOD;//Find factorial of Inverse Modulo of i under MOD 17 | 18 | int t; cin>>t; while(t--){ 19 | ll res=0,n,m,k; cin>>n>>m>>k; 20 | while(k--){ 21 | ll i,j,p; cin>>i>>j>>p; 22 | ll x1 = i+j-2; 23 | ll x2 = i-1; 24 | ll x3 = j-1; 25 | ll t1 = (fact[x1]*((inv[x2]*inv[x3])%MOD))%MOD;//Possible way to reach (i,j) from (1,1) 26 | ll y1 = n-i+m-j; 27 | ll y2 = n-i; 28 | ll y3 = m-j; 29 | ll t2 = (fact[y1]*((inv[y2]*inv[y3])%MOD))%MOD;//Possible way ro reach (n,m) from (i,j) 30 | res = (res + ((p*((t1*t2)%MOD))%MOD))%MOD;//All possible way to reach (n,m) from (1,1) passing from (i,j) multiplied by power 31 | //res = (res + mM(p,mM((fact[i+j-2]*(inv[i-1]*inv[j-1])%MOD)%MOD,(fact[n-i+m-j]*(inv[n-i]*inv[m-j])%MOD)%MOD)))%MOD; //TLE 32 | } 33 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | ll mpp[4]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 10 | ll n,res=0; cin>>n; 11 | for(int i=0;i>x; if(x<4) mpp[x]++; } 12 | ll a=mpp[1],b=mpp[2],c=mpp[3],d=n-a-b-c; 13 | res = (a*(a-1)*(a-2))>>1; 14 | res += a*(a-1)*b; 15 | res += ((a*(a-1))>>1)*(d+c); 16 | res += ((b*(b-1))>>1)*a; 17 | res += a*b*c; 18 | cout<2) res += 3*((fact[a]*((inv[3]*inv[a-3])%MOD))%MOD); 38 | if(a>1){res += b*2*((fact[a]*((inv[2]*inv[a-2])%MOD))%MOD); 39 | res += (n-a-b)*((fact[a]*((inv[2]*inv[a-2])%MOD))%MOD);} 40 | if(b>1) res += a*((fact[b]*((inv[2]*inv[b-2])%MOD))%MOD); 41 | res += a*b*c; 42 | cout< 3 | using namespace std; 4 | 5 | int nob(int i){ 6 | i=i-((i>>1)&0x55555555); 7 | i=(i&0x33333333)+((i>>2)&0x33333333); 8 | return (((i+(i>>4))&0x0F0F0F0F)*0x01010101)>>24; 9 | } 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 13 | int t; cin>>t; while(t--){int x,y; cin>>x; y=pow(2,nob(x)); cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX_A = 26; //Max alphabet 6 | const int MX = 20; //Max string size; 7 | 8 | ll fact[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | fact[0]=1; for(ll i=1;i<=MX;++i) fact[i]=fact[i-1]*i; 14 | 15 | ll t; cin>>t; while(t--){ 16 | string s; ll n,x=0; cin>>s>>n; sort(s.begin(),s.end()); 17 | 18 | ll freq[MX_A]={0},sum=0,k=0,len=s.size(); 19 | for(ll i=0;i=n){ out[k++]=i+'a'; n-=(sum-xsum); break; } 33 | if(sum=0;i--) if(freq[i]){ out[k++]=i+'a'; freq[i++]--; } 37 | for(ll i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 8 | 9 | int t; cin>>t; while(t--){ 10 | vector vec; 11 | ll res=0,n,p; cin>>n>>p; 12 | while(p--){ int x; cin>>x; vec.emplace_back(x); } 13 | int sz=vec.size(); 14 | for(int i=1;i<(1< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int arr[] = {2,3,11,13}; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 10 | 11 | ll res=0,n; cin>>n; int sz=sizeof(arr)/sizeof(0); 12 | for(int i=1;i<(1< 3 | typedef long long ll; 4 | using namespace std; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e5; 7 | 8 | ll res[MX+1], vec[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | int t; cin>>t; while(t--){ 14 | ll mx=-1,ans=0; int n; cin>>n; 15 | for(ll i=0;i>vec[i]; } 16 | for(ll i=0;i=0;--i){ mx=max(mx,vec[i]); ans+=min(res[i],mx-vec[i])%MOD; } 18 | cout<<(ans%MOD)<<"\n"; 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /MATHS/Inclusion Exclusion/evenGrid_E(BestSubmission).cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 2*1e6; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 12 | fact[0]=1,inv[0]=1,inv[1]=1; 13 | for(int i=1;i<=MX;++i) fact[i]=(fact[i-1]*i)%MOD; 14 | for(int i=2;i<=MX;++i) inv[i]=(-(MOD/i)*inv[MOD%i])%MOD+MOD; 15 | for(int i=2;i<=MX;++i) inv[i]=(inv[i]*inv[i-1])%MOD; 16 | ll k; 17 | int n,m,t; 18 | cin>>t; 19 | while(t--){ 20 | cin>>n>>m; k=n*m; 21 | cout<<(inv[k]*((fact[k<<1]*inv[k])%MOD))%MOD-1<<"\n"; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /MATHS/Inclusion Exclusion/iceCreamKhaloFraaaaandsss_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e6; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | unordered_map mpp; 10 | 11 | ll modMultiply(ll a, ll b){ 12 | ll res = 0; a %= MOD; 13 | while(b){ 14 | if(b & 1) res = (res + a) % MOD; 15 | a = (a<<1) % MOD, b>>=1; 16 | } 17 | return res; 18 | } 19 | 20 | ll power(ll x, ll y){ 21 | ll res=1; 22 | while(y>0){ 23 | if(y&1) res=modMultiply(res,x); 24 | y>>=1, x=modMultiply(x,x); 25 | } 26 | return res; 27 | } 28 | 29 | int main(){ 30 | ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 31 | 32 | fact[0]=1,inv[0]=1,inv[1]=1; 33 | for(int i=1;i<=MX;++i) fact[i]=(fact[i-1]*i)%MOD; 34 | for(int i=2;i<=MX;++i) inv[i]=(-(MOD/i)*inv[MOD%i])%MOD+MOD; 35 | for(int i=2;i<=MX;++i) inv[i]=(inv[i]*inv[i-1])%MOD; 36 | 37 | int t; cin>>t; while(t--){ 38 | ll k,n,l; string str; cin>>k>>n>>str; l=str.size()*n; 39 | if(k 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1e9+7; 6 | const int MX = 1e6; 7 | 8 | ll fact[MX+1],inv[MX+1]; 9 | unordered_map mpp; 10 | 11 | ll modMultiply(ll a, ll b){ 12 | ll res = 0; a %= MOD; 13 | while(b){ 14 | if(b & 1) res = (res + a) % MOD; 15 | a = (a<<1) % MOD, b>>=1; 16 | } 17 | return res; 18 | } 19 | 20 | ll power(ll x, ll y){ 21 | ll res=1; 22 | while(y>0){ 23 | if(y&1) res=modMultiply(res,x); 24 | y>>=1, x=modMultiply(x,x); 25 | } 26 | return res; 27 | } 28 | 29 | int main(){ 30 | ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 31 | 32 | fact[0]=1,inv[0]=1,inv[1]=1; 33 | for(int i=1;i<=MX;++i) fact[i]=(fact[i-1]*i)%MOD; 34 | for(int i=2;i<=MX;++i) inv[i]=(-(MOD/i)*inv[MOD%i])%MOD+MOD; 35 | for(int i=2;i<=MX;++i) inv[i]=(inv[i]*inv[i-1])%MOD; 36 | 37 | string str; cin>>str; int s = str.size(); 38 | for(int i=0;i0){ 41 | mpp[i+26]=fact[mpp[i]-1]; 42 | mpp[i]=fact[mpp[i]]; 43 | } 44 | 45 | ll n=fact[s-1]; 46 | for(int i=0;i<26;++i){ 47 | ll ans=1; 48 | for(int j=0;j<26;++j){ 49 | if(i==j) ans=(ans*mpp[j+26])%MOD; 50 | else ans=(ans*max(mpp[j],1))%MOD; 51 | } 52 | cout<<(n*(power(ans,MOD-2)%MOD))%MOD<<" "; 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /MATHS/Inclusion Exclusion/telephoneNetwork_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | ll mM(ll a, ll b){ 7 | ll res = 0; 8 | while(b){ 9 | if(b & 1) res = (res + a); 10 | a = (a<<1), b>>=1; 11 | } 12 | return res; 13 | } 14 | 15 | int main(){ 16 | int t; cin>>t; while(t--){ 17 | ll n; cin>>n; if(n<4){ cout<<"0\n"; continue; } 18 | n-=2; 19 | n=mM(n,n+1); 20 | n>>=1; 21 | n--; 22 | if(n<0) cout<<"0\n"; 23 | else cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | bool prime[2*MX];//2*MX for n=1000000 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false); cin.tie(NULL); 10 | 11 | memset(prime,true,sizeof(prime)); 12 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 13 | for(int i = 2; i <= MX; ++i) if(prime[i] and i%11!=1) prime[i]=false; 14 | prime[0] = false ,prime[1] = false; 15 | 16 | int t; cin>>t; 17 | while(t--){ 18 | int n; cin>>n; 19 | for(int i=n; i<=MX; ++i) if(prime[i]) { cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 10000000; 6 | 7 | double prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(14); 11 | for(int i=2;i<=sqrt(MX);++i) if(prime[i]==0) for(int j=i*i;j<=MX;j+=i) if(prime[j]==0) prime[j]=j; 12 | for(int i=2;i<=MX;++i){ 13 | if(prime[i]==0){ 14 | for(int j=2*i;j<=MX;j+=i) prime[j]*=(i-1.0)/i; 15 | prime[i]=i-1; 16 | } 17 | } 18 | prime[1]=1; 19 | for(int i=2;i<=MX;++i) prime[i]+=prime[i-1]; 20 | int t; cin>>t; while(t--){ int n; cin>>n; cout< 3 | using namespace std; 4 | 5 | bool prime(int n){ 6 | int cnt=0; 7 | for(int i=1;i*i<=n;++i){ 8 | if(n%i==0) { 9 | if(n==i*i) cnt++; 10 | else cnt+=2; 11 | } 12 | } 13 | if(cnt==2) return true; 14 | return false; 15 | } 16 | 17 | int main(){ 18 | ios_base::sync_with_stdio(false); cin.tie(NULL); 19 | int t; cin>>t; 20 | while(t--){ 21 | int n; cin>>n; 22 | if(prime(n+1) and n!=1) cout<<"NO\n"; 23 | else cout<<"YES\n"; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /MATHS/Primality Test/helpRachit_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 10000; 6 | const int MXX = 99980001; 7 | double res[MX]; 8 | 9 | bool isPrime(ll n){ 10 | if(n<2 or n%2==0) return false; 11 | if(n==2) return true; 12 | for(int i=3;i*i<=n;i+=2) if(n%i==0) return false; 13 | return true; 14 | } 15 | 16 | int main(){ 17 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 18 | 19 | int n=3,sq=n*n,pCnt=0,tCnt=0; 20 | for(int i=1;;++i){ 21 | int x = (i*(i+2)/4)+((i%4)/3)+1; 22 | if(x>sq){ res[n]=(100.0*pCnt)/tCnt; n+=2; sq=n*n; if(sq>MXX) break; } 23 | if(isPrime(x)) pCnt++; 24 | tCnt++; 25 | } 26 | 27 | int t; cin>>t; 28 | while(t--){ int nn; cin>>nn; printf("%0.6f\n",res[nn]); } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /MATHS/Primality Test/inLoveWithPrimes_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 7 | int t; cin>>t; 8 | while(t--){ int n; cin>>n; n==2?cout<<"Arjit\n":cout<<"Deepa\n"; } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /MATHS/Primality Test/jadooAndPrimes_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 100000000; 6 | //Do check the best submission it's insane 7 | 8 | bool prime[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | memset(prime,true,sizeof(prime)); 14 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 15 | prime[0] = false ,prime[1] = false; 16 | 17 | int t; cin>>t; 18 | while(t--){ 19 | unsigned long long res=0; int x; cin>>x; 20 | for(int i=0,j=0;j 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | long long modMultiply(long long a, long long b, long long MOD){ 7 | long long res = 0; a %= MOD; 8 | while(b){ 9 | if(b & 1) res = (res + a) % MOD; 10 | a = (2 * a) % MOD, b>>=1; 11 | } 12 | return res; 13 | } 14 | 15 | long long power(long long x, long long y, long long MOD){ 16 | long long res=1; 17 | while(y>0){ 18 | if(y%2==0) { y/=2; x=modMultiply(x,x,MOD); } 19 | else { y-=1; res=modMultiply(res,x,MOD); y/=2; x=modMultiply(x,x,MOD); } 20 | } 21 | return res; 22 | } 23 | 24 | int main(){ 25 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 26 | 27 | int t; cin>>t; 28 | while(t--){ 29 | unsigned long long a,d,p; cin>>a>>d>>p; if(d%p==0){cout<<"-1\n"; continue;} 30 | a%=p; d%=p; d=power(d,p-2,p); 31 | ll ans = modMultiply(-a,d,p); ans<0?cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | int prime[MX+1], res[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | 12 | for(int i = 2; i*i <= MX; ++i) if(prime[i]==0) for(int j = i*i; j <= MX; j+=i) prime[j] = 1; 13 | prime[1] = 1 ,prime[4] = 0; 14 | 15 | for(int i=1;i<=MX;++i) res[i]=res[i-1]+prime[i]; 16 | 17 | int t; cin>>t; 18 | while(t--){ int x,y; cin>>x>>y; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | int primePrime[MX+1]; 8 | bool prime[MX+1]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | memset(prime,true,sizeof(prime)); 14 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 15 | prime[0] = false ,prime[1] = false; 16 | 17 | for(int i=1;i<=MX;++i){ 18 | if(prime[i]) primePrime[i]++; 19 | primePrime[i]+=primePrime[i-1]; 20 | } 21 | 22 | for(int i=1,cnt=0;i<=MX;++i){ 23 | if(prime[primePrime[i]]) primePrime[i]=++cnt; 24 | else primePrime[i]=cnt; 25 | } 26 | 27 | int t; cin>>t; 28 | while(t--){ int x,y; cin>>x>>y; cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | const int preMX = 78498; 6 | 7 | int pr[MX+1], pre[preMX]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); 11 | int i,cnt=1; pre[0]=2; 12 | for(i=3;i*i<=MX;i+=2) if(pr[i]==0){ pre[cnt++]=i; for(int j=i*i;j<=MX;j+=(i<<1)) pr[j]=1;} 13 | for(;i<=MX;i+=2) if(pr[i]==0) pre[cnt++]=i; 14 | int t; cin>>t; while(t--){ int l,r,res=0,j=1; cin>>l>>r; for(auto p:pre){ res+=upper_bound(pre+j,pre+preMX,r/p)-lower_bound(pre+j++,pre+preMX,l/p+(l%p>0)); if(p*p>=r) break; } cout< 2 | using namespace std; 3 | typedef long long ll; 4 | const ll MX = 1e7; 5 | const ll preMX = 664579; 6 | 7 | ll spf[MX+1], pre[preMX+1]; 8 | 9 | ll solve(ll n,ll p){ 10 | ll res=1,x=n; for(int i=0;iMX) break; 21 | spf[idx]=pre[j]; 22 | } 23 | } 24 | 25 | int t; cin>>t; while(t--){ 26 | ll n,res=1,sres=1; cin>>n; 27 | while(n!=1){ 28 | ll nn=spf[n],times=0; while(n%nn==0){ n/=nn, times++; } 29 | res *= solve(nn,times), sres *= solve(nn*nn,times); 30 | } 31 | cout<<((res*res-sres)>>1)<<"\n"; 32 | } 33 | return 0; 34 | //nb= 35 | } 36 | -------------------------------------------------------------------------------- /MATHS/Primality Test/perfectCubes_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int nMX = 1000; 6 | const int pMX = 500; 7 | 8 | int sumpf[nMX+1][pMX+1],spf[pMX+1],idxs[pMX+1]; 9 | ll h1[nMX+1][nMX+1]; 10 | unordered_map mp; 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 14 | int idx=0; 15 | for(int i=2;i<=pMX;++i) if(spf[i]==0){ 16 | idxs[i]=idx++; 17 | for(int j=i;j<=pMX;j+=i) if(spf[j]==0) spf[j]=i; 18 | } 19 | 20 | int t; cin>>t; for(int i=0,k,x;i>k; while(k--){ 22 | cin>>x; for(int xx=idxs[spf[x]];x!=1;x/=spf[x],xx=idxs[spf[x]]) sumpf[i][xx]=++sumpf[i][xx]%3; 23 | } 24 | } 25 | 26 | ll res=0; 27 | for(int i=0;i 3 | using namespace std; 4 | const int MX = 1e4; 5 | 6 | bool prime[MX+1]; 7 | 8 | int main(){ 9 | memset(prime,true,sizeof(prime)); 10 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 11 | prime[0] = false ,prime[1] = false; 12 | 13 | int x,y; cin>>x>>y; 14 | for(int i=x;i<=y;++i) if(prime[i]) cout< 3 | using namespace std; 4 | const int MX = 1e6; 5 | 6 | bool pr[MX+1]; 7 | 8 | inline int ps(int x){ return x%10+((x%100)/10)+((x%1000)/100)+((x%10000)/1000)+((x%100000)/10000)+((x%1000000)/100000)+((x%10000000)/1000000); } 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | memset(pr,true,sizeof(pr)); pr[0]=false,pr[1]=false; 13 | for(int i=2;i*i<=MX;++i) if(pr[i]) for(int j=i*i;j<=MX;j+=i) pr[j]=false; 14 | int l,r; cin>>l>>r; for(int i=l;i<=r;++i) if(pr[i] and pr[ps(i)]) cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | 7 | bool isPrime(ll n){ 8 | if(n<2 or n%2==0) return false; 9 | if(n==2) return true; 10 | for(int i=3;i*i<=n;i+=2) if(n%i==0) return false; 11 | return true; 12 | } 13 | 14 | int main(){ 15 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 16 | int t; cin>>t; 17 | while(t--){ ll n; cin>>n; isPrime(n)?cout<<"prime\n":cout<<"composite\n"; } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /MATHS/Primality Test/primeString_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | const int MX = 100000; 5 | 6 | bool prime[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 10 | 11 | memset(prime,true,sizeof(prime)); 12 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 13 | prime[0] = false ,prime[1] = false; 14 | 15 | int t; cin>>t; 16 | while(t--){ 17 | vector vec; unordered_map mpp; bool fl=true; int cnt=0; string str; cin>>str; 18 | for(int i=0;i 3 | using namespace std; 4 | typedef long long ll; 5 | const int MOD = 1000000007; 6 | const int MX = 2000000; 7 | 8 | bool prime[MX]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 12 | 13 | memset(prime,true,sizeof(prime)); 14 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 15 | 16 | int n,i,j; ll res=0; cin>>n; n--; if(n<2){ cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | int prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | 12 | for(int i = 2; i <= MX; ++i) if(prime[i]==0) for(int j = i; j <= MX; j+=i) if(prime[j]==0) prime[j] = i; 13 | prime[0] = 0 ,prime[1] = 1; 14 | 15 | int t; cin>>t; 16 | while(t--){ int x; cin>>x; cout< 3 | using namespace std; 4 | const int MX = 1000000; 5 | 6 | bool prime[MX+1]; 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 10 | 11 | memset(prime,true,sizeof(prime)); 12 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 13 | prime[0] = false ,prime[1] = false; 14 | 15 | int t; cin>>t; 16 | while(t--){ 17 | int n,k,res=0,cnt=1; cin>>n>>k; 18 | while(n--){ int x; cin>>x; if(prime[x] and cnt++%k==0) res+=x; } 19 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | 6 | int noOfDiv(int l, int r){ 7 | int arr[r-l+1],mx=-1e9,cnt=0; memset(arr,0,sizeof(arr)); 8 | for(int i=1;i*i<=r;++i){ 9 | int srt = (l/i)*i>=l?(l/i)*i:(l/i+1)*i, sq=i*i; 10 | for(int j=srt;j<=r;j+=i){ 11 | if(jmx){ cnt=1; mx=arr[i-l];} 18 | else if(arr[i-l]==mx) cnt++; 19 | } 20 | return cnt; 21 | } 22 | 23 | int main(){ 24 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 25 | int t; cin>>t; 26 | while(t--){ int l,r; cin>>l>>r; cout< 3 | using namespace std; 4 | const int MX = 100000; 5 | const int Inf = 1e7; 6 | 7 | int res[5*MX+1],vec[5*MX+1]; 8 | 9 | bool prime(int n){ 10 | if(n==0 or n==1) return false; 11 | if(n!=2 and n%2==0) return false; 12 | for(int i=2; i*i<=n; ++i){ if(n%i==0) return false; } 13 | return true; 14 | } 15 | 16 | int main(){ 17 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 18 | 19 | int t; cin>>t; 20 | for(int i=1;i<=t;++i){ int n; cin>>n; vec[i]=n; } 21 | 22 | int cur=Inf; 23 | for(int i=1;i<=t;++i){ 24 | if(prime(vec[i])) cur=i; 25 | res[i]=cur; 26 | } 27 | for(int i=t;i>=1;--i){ 28 | if(prime(vec[i])) cur=i; 29 | res[i]=abs(res[i]-i)<=abs(cur-i)?res[i]:cur; 30 | } 31 | for(int i=1;i<=t;++i) res[i]==Inf?cout<<"-1 ":cout< 3 | int main(){ std::cout<<"6211 6301"; return 0; } 4 | -------------------------------------------------------------------------------- /MATHS/Primality Test/shwetaRiddhimaAndChocolates-B_M.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | int main(){ std::cout<<"49280963 49281161"; return 0; } 4 | -------------------------------------------------------------------------------- /MATHS/Primality Test/smallestNumber_E.cpp: -------------------------------------------------------------------------------- 1 | //@author Nikhil Londhe 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | bool prime[MX+1]; 8 | vector factors; 9 | 10 | void genPrime(){ 11 | memset(prime,true,sizeof(prime)); 12 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 13 | prime[0] = false ,prime[1] = false; 14 | } 15 | 16 | void primeFac(int n){ 17 | while(n%2==0){ prime[2]=false; n>>=1; } 18 | for(int i=3;i*i<=n;i=i+2) while(n%i==0){ prime[i]=false; n/=i; } 19 | if(n>2) prime[n]=false; 20 | } 21 | 22 | int main(){ 23 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 24 | int t; cin>>t; 25 | while(t--){ 26 | genPrime(); bool fl=true; int n; cin>>n; 27 | while(n--){ 28 | int x; cin>>x; if(x==0) continue; //Some test cases have 0 29 | if(prime[x]) prime[x]=false; 30 | else primeFac(x); 31 | } 32 | for(int i=1;i<=MX;++i) if(prime[i]){ fl=false; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | int prime[MX+1],sOD[MX+1],smith[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | 12 | for(ll i=2;i*i<=MX;i++) if(prime[i]==0) for(ll j=i*i;j<=MX;j+=i) if(prime[i]==0) prime[j]=i; 13 | for(ll i=1;i<=MX;i++) if(prime[i]==0) prime[i]=i; 14 | 15 | for(ll i=1;i<=MX;i++){ 16 | if(i%10==0){ ll n=i; while(n){ sOD[i]+=n%10; n/=10; }} 17 | else sOD[i]=sOD[i-1]+1; 18 | } 19 | 20 | for(int i=1;i<=MX;i++){ 21 | ll sum=0,j=i; while(j>1){ sum+=sOD[prime[j]]; j/=prime[j]; } 22 | smith[i]=sum==sOD[i]?smith[i-1]+1:smith[i-1]; 23 | } 24 | 25 | int t; cin>>t; while(t--){ int l,r; cin>>l>>r; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1e6; 6 | 7 | bool prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | memset(prime,true,sizeof(prime)); prime[0]=false,prime[1]=false; 12 | for(int i=2;i*i<=MX;++i) if(prime[i]) for(int j=i*i;j<=MX;j+=i) prime[j]=false; 13 | int t; cin>>t; while(t--){ 14 | int x; cin>>x; if(x<5){ cout<<"No\n"; continue; } 15 | if(x&1){ cout< 3 | using namespace std; 4 | const int MX = 1000000; 5 | const long long Inf = 1e9; 6 | bool prime[MX+1]; 7 | 8 | template void ckmin(T &a, T b) { a = min(a, b); } 9 | template void ckmax(T &a, T b) { a = max(a, b); } 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 13 | 14 | memset(prime,true,sizeof(prime)); 15 | for(int i = 2; i*i <= MX; ++i) if(prime[i]) for(int j = i*i; j <= MX; j+=i) prime[j] = false; 16 | prime[0] = false ,prime[1] = false; 17 | 18 | int t, mx=-Inf, mn=Inf; cin>>t; 19 | while(t--){ int x; cin>>x; if(prime[x]){ ckmin(mn,x); ckmax(mx,x); } } 20 | if(mx==-Inf or mn==Inf){ cout<<"-1"; } 21 | else{ cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 5000000; 6 | const int MOD = 100000007; 7 | 8 | bool prime[MX+1]; 9 | int res[MX+1]; 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 13 | 14 | memset(prime,true,sizeof(prime)); 15 | for(ll i = 2; i*i <= MX; ++i) if(prime[i]) for(ll j = i*i; j <= MX; j+=i) prime[j] = false; 16 | prime[0] = false ,prime[1] = false, prime[2] = false; 17 | 18 | for(ll i=0;i<=MX;i++) res[i]=prime[i]?(res[i-1]+(i>>1))%MOD:res[i-1]; 19 | 20 | int t; cin>>t; while(t--){ ll x; cin>>x; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000; 6 | const int MXX = 3700000; 7 | 8 | bool prime[MXX]; 9 | int res[MX+1]; 10 | vector vec; 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 14 | 15 | memset(prime,true,sizeof(prime)); 16 | for(int i = 2; i*i <= MXX; ++i) if(prime[i]) for(int j = i*i; j <= MXX; j+=i) prime[j] = false; 17 | prime[0] = false, prime[1] = false; 18 | 19 | int sum=0, start=0, ans=0; 20 | for(int i=1;i<=MX;++i){ 21 | if(prime[i]) vec.emplace_back(i); 22 | else{ res[i]=ans; continue; } 23 | if(vec.size()<2){ res[i]=ans; continue;} 24 | start=0, ans=0; unordered_map mpp; 25 | for(int k=1;k0 and prime[sum] and !mpp[sum]) ans++; 29 | mpp[sum]=true; 30 | } 31 | } 32 | res[i]=ans; 33 | } 34 | int t; cin>>t; 35 | while(t--){ int n; cin>>n; cout< 3 | using namespace std; 4 | typedef long long ll; 5 | const int MX = 1000000; 6 | 7 | bool prime[MX+1]; 8 | 9 | int main(){ 10 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 11 | 12 | memset(prime,true,sizeof(prime)); 13 | for(int i=2;i*i<=MX;++i) if(prime[i]) for(int j=i*i;j<=MX;j+=i) prime[j]=false; 14 | prime[0]=false, prime[1]=false; 15 | 16 | int n; cin>>n; double res=n; 17 | for(int i=1;i<=n;++i) if(prime[i] and n%i==0) res*=(1-(1.0/i)); 18 | cout<