Calculating number of intersections of a horizontal line with line segments efficiently

I’m given an array $ A = [a_1, a_2, ….a_n] $ using which I construct $ n-1$ contiguous line segments by drawing a line from $ (i,a_i)$ to $ (i+1, a_{i+1})$ . Now, I’m given $ q$ queries in the form of $ x_1, x_2, y, l, r$ where $ l$ and $ r$ are the range for the array $ A$ and the rest indicate a horizontal line segment $ L$ from $ (x_1, y)$ to $ (x_2, y)$ . For each query, I want to find total intersections of $ L$ and the segments in the range $ l$ and $ r$ in $ O(n+q)$ or $ O(n + q\log{n})$ complexity.
I was able to arrive at a solution that works in $ O(nq)$ which simply traverses each range and calculates whether $ L$ intersects with the segments or not.
I believe, some pre-processing can be done on $ A$ which can reduce the complexity.
Any leads will be appreciated!

MMA has an error in calculating the minimum polynomial of a rational number

The minimum polynomial of Power[2, (3)^-1] (-(1/2) + (I Sqrt[3])/2) Python minimal_polynomial function can get the result as x^2 + 2^(1/3)*x + 2^(2/3) .(In Python ** means^) enter image description here

import os import sympy as sy from sympy import minimal_polynomial, sqrt, solve, QQ, Rational #Rational  from import x, y, z s = minimal_polynomial (2 ** Rational (1,       3)*(Rational (-1, 2) +        Rational (1, 2)*3 ** Rational (1, 2)*sqrt (-1)), x,     domain = QQ.algebraic_field (2 ** Rational (1, 3))) print (s) print (sy.latex (s)) 

But MMA can’t get the result.

MinimalPolynomial[Power[2, (3)^-1] (-(1/2) + (I Sqrt[3])/2), x,   Extension -> Power[2, (3)^-1]] 

Can someone explain me the Credit-Debit proof method for calculating operations?

I’ve started taking a data structure course and we are currently learning about different data structures. We also learned when to increase the capacity of an array by creating another array with double the size of the original one.

The proof used to demonstrate this was a credit-debit proof where each operations has a cost. ex: doubling an array cost 2 dollars reducing an array by half cost 1 dollar.

I simply don’t understand how proof of that type works. Doing a quick online search I haven’t find the term credit-debit used to refer to this. Maybe It’s not its name? Would be nice if someone could give me some links that explain those proofs.

Calculating HP for UA beast master animal companion

I am a very new DM in DnD 5e, and one of my players is a ranger. As I was browsing through posts here and there we decided to use the UA, as everyone is saying the RAW ranger is lackluster. She obtained her wolf in a little side story, and I would like to set up the stats for her companion. I read through the UA, and it states:

For each level you gain after 3rd, your animal
companion gains an additional hit die and increases its hit points accordingly.

Now this confuses me a lot, and upon further looking into how HP is calculated, I don’t even understand the stats of the simple Wolf enemy.

So in the Monster Manual the HP of the wolf is 11 (2d8+2).

First question: How is this calculated? The average of 2d8 should be 10. 10+2 is 12.

Second question: What is the hit die of a wolf? If its 1d8, than in case the wolf is “level 1”, its HP should be 10 (max hit die + con modifier). If it is considered to be level 2, its HP should be 17 (Max hit die + avg hit die + con modifier). What am I missing?

Third question: I found a post on a DnD reddit site where someone calculates the stats of a wolf in case the ranger is level 7. It goes like this:


Medium beast, your alignment

Armor Class 17

Hit Points 35 (6d8+6)

Speed 40 ft.

STR 12 (+1)

DEX 17 (+3)

CON 12 (+1)

INT 3 (-4)

WIS 12 (+1)

CHA 6 (-2)

Proficiency Bonus +3

Skills: Perception +4, Stealth +6, Skill of Choice, Skill of Choice

Saving Throws: Str +4, Dex +6, Con +4, Int -1, Wis +4, Cha +1

Senses: passive Perception 14

Actions Bite. Melee Weapon Attack: +6 to hit, reach 5 ft., one target. Hit: 11 (2d4 + 6) piercing damage. If the target is a creature, it must succeed on a DC 12 Strength saving throw or be knocked prone.

I think I was able to figure out everything, except the HP.

My first assumption was to start with the given base 11 HP as a level 3 companion. Than at level 4, it gains an additional 7 (5 hit die +con modifier). And the same thing goes on for level 5, 6 and 7, to a total of 11+4*7 totaling 39.

If I start from the 2d8+2, and just add a hit die each time the ranger levels up, I can understand the 6d8 mentioned in the post, but I don’t get the +6 modifier. Shouldn’t it be +10? +2 base modifier at level 3, and +2 for each 4 levels totaling at +10. And now the problem is that averaging this value leads to 40, not 39, this problem is coming from my first question.

I am completely lost here.

One last question. The Bite attack says +6 to hit. I can explain this as the DEX modifier became +3, and the proficiency bonus also became +3, however I dont understand the reason why DEX modifier is used for the Bite attack instead of STR.

Thanks a lot for the answer!

Calculating match % and ranking according to that

I’m creating a website like where users will answer some yes/no questions set by me, up to them how many of those questions they want to answer. After a user submits his answer(s), he will be shown top 5 matches along with their match percentages. If two users have 10 common questions and their answers match for 8 of those questions then their match % will be 80%.

I can make this but my concern is about efficiency. A way of making this: If a user wants to see his top matches then match % (or match ratio) will be calculated for him vs every other user in the system. This will be stored in a temporary array. Array is sorted. Top 5 matches from the array are displayed.

Any less resource intensive way to calculate and show top matches?

Calculating effective access time in memory caching context

I have went through various problems involving time required to access required data in the context of caching.

They use different formulae in different problems. For example, this answer suggests these formulae:

effective-access-time = hit-rate * cache-access-time                   + miss-rate * lower-level-access-time    effective-access-time = cache-access-time + miss-rate * miss-penalty 

Let me rewrite them:

  1. $ T_{eff}=H_{L1}\times T_{L1} + (1-H_{L1})\times T_{LowerLevelMemories}$
  2. $ T_{eff}=T_{L1}+(1-H_{L1})\times T_{L1MissPenalty}$

$ H_{L1}$ is L1 cache hit rate
$ T_L1$ is L1 cache access time
$ T_{LowerLevelMemories}$ is time to access lower level memories
$ T_{L1MissPenalty}$ is L1 cache miss penalty

About when to use which formula, the answer says:

  • use first formula when lower level memory access time is given and
  • 2nd formula when miss penalty is given.

Is it so?

Let me put $ T_{LowerLevelMemories}=T_M$ , a main memory access time in formula 1. Also, I feel formula 1 ignores that we do indeed access L1 cache when cache miss occurs. So we should also multiply $ (1-H_{L1})$ by $ T_{L1}$ . So formula 1 becomes:

$ T_{eff}=H_{L1}\times T_{L1} + (1-H_{L1})\times (T_M+T_{L1})$ $ =H_{L1}\times T_{L1} + T_M+T_{L1} -H_{L1}\times T_M-H_{L1}\times T_{L1}$
$ =T_{L1}+(1-H_{L1})\times T_M$

This last equation resembles formula 2 above. Also we can interpret that $ T_M$ is exactly the L1 miss penalty. So I feel, we should

  • use formula 2 when we assume L1 is accessed even during miss and
  • formula 1 when we assume L1 is not accessed during miss

However this does not align with what answer has to suggest as quoted above. Am I correct with how to make decision when to use which formula or the quoted suggestion from the answer is correct?

Calculating Time for a Attack on Password Hashes

Suppose I have a database containing hashes. Hashes are obtained from password developed from randomly chosen set of 94 characters such that each password size is of 8 characters. So we have 94^8 passwords. Each of the passwords are converted into hashes i.e 94^8 hashes and stored in the database. Now the attacker gets access to the hashes but he can’t reverse the hashes. So he used pre-computed hashes stored in the rainbow table. How can we calculate the time for finding a match between the hashes stored in the DB and the hashes stored in the rainbow table? Somebody please guide me.

What are the stat modifiers (other than Dex) that can be used when calculating AC?

As far as my knowledge of D&D goes there are 2 ways to determine AC:

1. Calculation of AC through Base + Stat modifiers

Examples of these AC calculations are:

  • Unarmored = 10 + Dex mod
  • Unarmored defense, such as Barbarian = 10 + Con mod + Dex mod
  • Light armors, such as Leather Armor = 11 + Dex mod
  • Medium armors, such as Breastplate = + Dex mod (max 2)
  • Spells, such as Mage Armor (AC = 13 + Dex mod)

2. Setting the base AC to a specific value

Examples of these AC calculations are:

  • Heavy Armor, such as Plate Armor = 18
  • Racial, such as Tortle’s Natural armor race = 17
  • Spells such as Barkskin = 16

My interest goes out to the first way of calculating AC.

What are the stat modifiers (other than Dex) that can be used when calculating AC?

I already found 2:

  • Barbarian’s Unarmored Defense = 10 + DEX mod + WIS mod
  • Monk’s Unarmored Defense = 10 + DEX mod + WIS mod

Are there any others that I missed?

Am I correctly calculating the damage of this build?

Books of D&D 3.5:

  • Player’s Handbook (PHB)
  • Tome of Magic (ToM)
  • Complete Adventurer (CA)
  • Complete Psionic (CP)
  • Expanded Psionic (EP)

My character is a fighter 2/shadowcaster 1/scout 1/ardent 1. He had his hand nibbled off so he is one-handed, but can use the stump to cast spells like dust arrow (from ToM, which is a super-natural ability). He also has an “off-hand” bite attack that does 1d4 damage.

I have a plan to do this:

  • Skirmish 2d6 (+2d6 Improved Skirmish) = 4d6
  • Psionic Shot + Improved Psionic Shot = 4d6
  • Dust arrow = 2d4

If I use dimension hop to trigger Skirmish, and shoot with two rays, then it will do 4d6 (Skirmish) + 4d6 (Skirmish) + 4d6 (Psionic Shot) + 2d4 + 2d4 = a maximum of 88 damage.

Are my calculations right?