BNU 34975 paper-cut broken line partition plane problem large number simulation + law

Source: Internet
Author: User

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;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.