Segment---hdu5666 (two long long to find remainder of long long)

Source: Internet
Author: User
Tags mul

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5666

Test instructions: There is a line x+y=n, to find the line and the axis of the triangle enclosed by how many integer points, not on the axis and line, so that the result of the P-remainder

The obvious answer is (1+2+3+...+a-2)%P = ((A-1) (a-2)/2)%P;

Because the range of a and p are relatively large, the direct multiplication will explode ll's, so you can use the idea of a fast power matrix;

X*y = 2*X*Y/2; Y is an even number;

X*y = x + 2*x*y/2; Y is odd;

So there's a bit of code:

#include <iostream> #include <vector> #include <stdio.h> #include <string.h> #include < stdlib.h> #include <algorithm> #include <math.h>using namespace std; #define N 1050#define PI 4*atan (1) # Define met (A, b) memset (A, B, sizeof (a)) typedef long Long LL; ll Mul (ll X, ll y, ll Mod) {    if (y = = 0) return 0;    LL ans = 2 * Mul (x, Y/2, Mod)%mod;    if (y%2)        ans = (ans+x)%mod;    return ans;} int main () {    int T;    scanf ("%d", &t);    while (t--)    {        LL ans, p, q, N, mod;        scanf ("%i64d%i64d", &n, &mod);        p = n-1; Q = n-2;        if (p&1)            ans = Mul (p, Q/2, mod);        else            ans = Mul (P/2, q, mod);        printf ("%i64d\n", ans);    }    return 0;}

  

Segment---hdu5666 (two long long to find remainder of long long)

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.