algorithm

how to calculate a^(b^c) mod n? [duplicate]

时光毁灭记忆、已成空白 提交于 2021-02-08 04:37:27
问题 This question already has answers here : finding a^b^c^… mod m (6 answers) The most efficient way to implement an integer based power function pow(int, int) (17 answers) Closed 6 years ago . Can someone tell me the efficient way to solve this problem ? a b c mod m a, b, c, and m are 32 bit int where m is not a prime number and a is not coprime to m ?I've looking for the answer, but haven't found it There are some answered questions that familiar with this question, but none of them can

What is the best way to stock products with different purchase prices & expiration date?

╄→尐↘猪︶ㄣ 提交于 2021-02-08 04:37:24
问题 I'm developing a stock inventory system using PHP, Mysql. This system have purchase module to add/update/increase quantity and expire date of products & sale module to decrease/out the quantity of products. Now what is creating confusion to me and what is my real question is that Suppose we have PRODUCT A1 in our stock with quantity 20 and the price of PRODUCT A1 was $15 when we purchase it. Now we want to stock this product more for example we want to buy 100 more items of PRODUCT A1 but now

Maximum subarray of size HxW within a 2D matrix

心已入冬 提交于 2021-02-08 03:45:56
问题 Given a 2-dimensional array of positive integers, find the subrectangle of size HxW with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. Input: A 2D array NxN with positive elements The HxW size of the subrectangle Output: The submatrix of HxW size with the largest sum of its elements. I've solved this using a brute-force method, however, I'm now looking for a better solution with better complexity (my brute-force method's complexity is O(n 6 )). 回答1:

Whats the best algorithm for Non Disjoint Set Union?

♀尐吖头ヾ 提交于 2021-02-08 03:32:25
问题 Lets say there are two (non disjoint) sets of points (cartesian space), what is the best case complexity algorithm to perform the union of the two sets ? 回答1: Since the point coordinates are arbitrary and there is no special relation between them, I don't see this problem as a geometric specific problem. It is the generic problem of efficiently merging S1 and S2 into a new set S. I know about two options: 1) When the sets are stored in a hash table (actually a hash set), the union takes O(|S1

Calculate absolute difference |A-B| in assembly using only INC, DEC, JNZ, HALT - interview question

泄露秘密 提交于 2021-02-08 03:31:55
问题 This is a question I encountered in an interview and for which I didn't find a solution - so I tried to solve it myself. We can use pseudocode here - it doesn't need to be a formal code. The question is to get the absolute difference of unsigned inputs: Assume your assembly language includes ONLY the following instructions: inc , dec , jnz and halt ( halt = stop running). Task: A and B are registers that hold non-negative values. The program should calculate the value of |A-B| and locate the

Whats the best algorithm for Non Disjoint Set Union?

…衆ロ難τιáo~ 提交于 2021-02-08 03:31:28
问题 Lets say there are two (non disjoint) sets of points (cartesian space), what is the best case complexity algorithm to perform the union of the two sets ? 回答1: Since the point coordinates are arbitrary and there is no special relation between them, I don't see this problem as a geometric specific problem. It is the generic problem of efficiently merging S1 and S2 into a new set S. I know about two options: 1) When the sets are stored in a hash table (actually a hash set), the union takes O(|S1

How to interpolate N points that do not describe a function

隐身守侯 提交于 2021-02-08 02:01:59
问题 Suppose i have n points, also suppose that this points have an order, and not necessary this points make a function. I'm wondering how to interpolate them if the points do not describe a function? For example this will be the original points: And i hope this result: Note that only using splines do not works because the points do not make a function, and also using Bezier curves do not works because them do not interpolate the points (only pass near of them). How can i do for get this? Is

Find point with vector projection

ぐ巨炮叔叔 提交于 2021-02-07 23:14:03
问题 I am trying to work this out in java with LatLng point I was looking at this post here Circle line-segment collision detection algorithm? I have a method to find distance between 2 point. The instruction says Project the vector AC onto AB. The projected vector, AD, gives the new point D. If the distance between D and C is smaller than (or equal to) R we have an intersection. I don't have knowledge about vector, could anyone help me how to find point D here ? Thanks in advance 回答1: If you

Sorting pairs of teams with non-repeating | Round-robin tournament

拜拜、爱过 提交于 2021-02-07 22:56:45
问题 I'm generating schedule for the tournament. Each team should play exactly 8 games. Number of teams 2 < n < 36 For sorting teams into pairs I'm using Round Robin algorithm to get a table, example of 6 teams : Then I convert it into the set of pairs: 1 4 2 3 3 2 4 1 5 6 6 5 1 2 2 1 3 5 4 6 5 3 6 4 ... The question is how to sort this set, in order to get schedule, where the same team can't play 2 games in a row. But if it's impossible, minimize the number of exceptions. Example with new

Print ways to reach the n’th stair

让人想犯罪 __ 提交于 2021-02-07 20:32:31
问题 I recently encountered this problem in an interview There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Print all possible ways person can reach the top. For example, n=4 Output: 1 2 3 4 1 2 4 1 3 4 2 3 4 2 4 But I couldn't code this properly. How to code up solution for this? 回答1: To print the number of ways, you can first understand how to calculate the number of ways, and adjust it so each "count" will print