X ^ 0 + x ^ 1 + x ^ 2 +... x ^ n mod P; X, N, P <= 10 ^ 9

Source: Internet
Author: User

Method 1: quick power. But it must have timed out.

Method 2: Use the proportional sequence formula, but there is division.

Method 3: Sub-governance taste ..

If n is an even number, x ^ 0 + x ^ 1 + x ^ 2 + ....... x ^ n = (x ^ 0 + x ^ 1 + x ^ 2 + ..... x ^ (n/2) * (1 + x ^ (n/2)-x ^ (n/2); that is, F (N) = f (n/2) * (1 + x ^ (n/2)-x ^ (n/2 );

When N is an odd number, x ^ 0 + x ^ 1 + x ^ 2 + ....... x ^ n = (x ^ 0 + x ^ 1 + x ^ 2 + ..... x ^ (n/2) * (1 + x ^ (n/2)-x ^ (n/2) + x ^ N; that is, F (N) = f (n/2) * (1 + x ^ (n/2)-x ^ (n/2) + x ^ N;

The scale of N can be halved in each recursion ..

It's just your own ideas. You are welcome to point out errors or propose better methods.

After the test, the subtraction is also consistent, that is, a % P-B % P + p) % P = (a-B) % P.

 

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.