[CODEVS 1281] Xn series, codevs1281xn Series

Source: Internet
Author: User

[CODEVS 1281] Xn series, codevs1281xn Series

Description

6 numbers, m, a, c, x0, n, g
Xn + 1 = (aXn + c) mod m, evaluate Xn
Http://codevs.cn/problem/1281/

Analysis

This is a bare matrix multiplication question. I haven't done it for a long time.

Assume that the matrix A is {a1, a2}, {a3, a4 }}, B = {b1, b2}, {b3, b4 }}.
Calculation methods based on matrix multiplication include:
A × B = {a1b1 + a2b2, a1b2 + a2b4}, {a3b1 + a4b3, a3b2 + a4b4 }}.
So what we want is a * Xn + c = Xn + 1.
Assuming that X exists in a1, Xn * b1 + a2 * b2 = Xn + 1
Then we can get a2 = 1, b1 = a, b2 = c.
(Why can't a2 = c, b2 = 1? Because a2 is changed after multiplication, the next addition is not c ).

In this way, the matrix is created, and the next step is step-by-step matrix multiplication.
However, the data here is too big, and unsigned long will overflow without special skills. High Precision? Actually, this is not necessary, because we have to model it at last.

Special skills

Write a quick addition using a method similar to the quick power, and add a modulo to avoid the overflow of the direct multiplication.

Code

20 ms 256kB
Amount... Are mod and sum reserved words? Why are all highlighted?

#include<cstdio>using namespace std;typedef long long LL;LL mod, a, c, x0, n, g;struct Matrix {    LL m[2][2];} base, X0;// return a * bLL quickadd(LL a, LL b) {    LL ans = 0;    a %= mod; b %= mod;    while(b > 0) {          if(b & 1) ans = (ans + a) % mod;          a = (a + a) % mod;          b >>= 1;    }    return ans;}Matrix mul(Matrix a, Matrix b) {    Matrix ans;    for(int i = 0; i < 2; i++)        for(int j = 0; j < 2; j++) {            LL sum = 0;            for(int k = 0; k < 2; k++)                sum = (sum + quickadd(a.m[i][k], b.m[k][j])) % mod;            ans.m[i][j] = sum;        }    return ans;}Matrix pow(Matrix a, LL n) {    Matrix p = {{1, 0, 0, 1}};    while(n > 0) {        if(n & 1) p = mul(p, a);        a = mul(a, a);        n /= 2;    }    return p;}int main() {    scanf("%lld%lld%lld%lld%lld%lld", &mod, &a, &c, &x0, &n, &g);    base = (Matrix) {{a, 0, 1, 1}};    X0 = (Matrix){{x0, c, 0, 0}};    Matrix ans = mul(X0, pow(base, n));    printf("%lld\n", ans.m[0][0] % g);    return 0;}

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.