BNU 34975 paper-cut broken line partition plane problem large number simulation + law
Question link: Click the open link
The most common condition is that each line and all lines in the current plane are intersecting.
There are currently x straight lines
Then, a line with a type0 can generate x intersections. The line segments between the two intersections can divide a plane into two
You can add x + 1 plane.
Promote the line if typeY is added.
Let Y ++ first, indicating the number of elements that are added to a straight line.
You can add (x + 1) * Y-(Y-1)
After adding, the number of straight lines on the plane increases by Y: that is, x + = Y.
Therefore, each time x y-type lines are added
The answer is:
Y ++;
Ans + = (x + 1) * Y-(Y-1)
Ans + = (x + 1 + Y) * Y-(Y-1)
·
·
·
Ans + = (x + 1 + (X-1) * Y-(Y-1)
And then simply
Ans + = x * Y + 1
Ans + = (x + Y) * Y + 1
·
·
·
Ans + = (x + (X-1) * Y + 1
We found that ans added a total of X 1, and the first part was Y * (arithmetic difference series)
And then simplified
Ans + = X + Y * (x + (X-1) * Y) * X/2 );
In this way, we can O (1) calculate the value of each added answer
However, the modulo operation may fail.
/2, the reverse element may not exist,
But it can ensure that (x + (X-1) * Y) * X must be an even number, so use a large number of direct calculation.
import java.math.*;import java.util.*;import java.io.*;public class Main { static BigInteger x1 = new BigInteger("1"); static BigInteger x2 = new BigInteger("2"); public void work() { int T; T = cin.nextInt(); while (T-- > 0) { int n = cin.nextInt(); long mod = cin.nextLong(); long x = 0; long ans = 1 % mod; while(n-->0) { long X = cin.nextLong(), Y = cin.nextLong(); Y++; BigInteger now = BigInteger.valueOf(2*x + (X-1)*Y); now = now.multiply(BigInteger.valueOf(X)); now = now.divide(BigInteger.valueOf(2)); now = now.mod(BigInteger.valueOf(mod)); long tmp = now.longValue(); tmp *= Y; tmp%=mod; tmp += X; tmp%=mod; ans += tmp; ans %= mod; x += X*Y%mod; x %=mod; } ans = ans % mod; System.out.println(ans); } } Main() { cin = new Scanner(System.in); } public static void main(String[] args) { Main e = new Main(); e.work(); } public Scanner cin;}