For each triangular region of a weighted infinite triangle grid, compute the total cost of all cells in the region modulo 1e9+7.
Medium7CombinatoricsMathPrefix sumNumber theoryNo attempts yetTime limit2sMemory limit512 MBA game that Hyukjung plays added a sixth job advancement. Its skill tree is an equilateral triangular grid that continues downward without end.
Row x of the grid holds x cells, and the y-th cell from the left in that row is written (x,y). The cell below and to the left of (x,y) is (x+1,y), and the cell below and to the right is (x+1,y+1).
Learning the skill at the top vertex (1,1) costs 1 won. If the skill in some cell costs k won, the cell below and to the left costs Ak won and the cell below and to the right costs Bk won.
The skill tree has N special triangles. The special triangle written as the three numbers xi, yi, mi takes cell (xi,yi) as its top vertex and covers mi rows. That is, for each j with 0≤j≤mi−1, the j+1 cells from (xi+j,yi) through (xi+j,yi+j) belong to that triangle. Every time you learn all the skills inside one special triangle, your total damage doubles.
Help Hyukjung and find the cost of learning every skill inside each special triangle.
The first line holds A and B, separated by a space. (1≤A,B≤109)
The second line holds the number of special triangles N. (1≤N≤100000)
Each of the next N lines holds three integers xi, yi, mi separated by spaces, describing one special triangle. (1≤yi≤xi≤1018, 1≤mi≤1018)
Print N lines. On each line print the cost of learning every skill inside the corresponding special triangle, modulo 109+7, in the order the triangles are given.