zoukankan      html  css  js  c++  java
  • [洛谷P2044][NOI2012]随机数生成器

    题目大意:给你$m,a,c,X_0,n,g$,求$X_{n+1}=(acdot X_n+c) mod{m}$,最后输出对$g$取模

    题解:矩阵快速幂+龟速乘,这里用了$long;double$强转

    卡点:

    C++ Code:

    #include <cstdio>
    #include <cmath>
    using namespace std;
    long long m, a, c, x0, n, g;
    long long mul(long long a, long long b) {
    	long long d = (long long) floor(a * (long double) b / m + 0.5);
    	long long res = a * b - d * m;
    	if (res < 0) res += m;
    	return res;
    }
    //long long mul(long long a, long long b) {
    //	long long res = 0;
    //	while (b) {
    //		if (b & 1) res = (res + a) % m;
    //		b >>= 1;
    //		a = (a + a) % m;
    //	}
    //	return res;
    //}
    struct matrix {
    	long long s[4];
    	matrix (long long a, long long b, long long c, long long d) {
    		s[0] = a; s[1] = b; s[2] = c; s[3] = d;
    	}
    	matrix operator * (matrix rhs) {
    		matrix res(0, 0, 0, 0);
    		res.s[0] = (mul(s[0], rhs.s[0]) + mul(s[1], rhs.s[2])) % m;
    		res.s[1] = (mul(s[0], rhs.s[1]) + mul(s[1], rhs.s[3])) % m;
    		res.s[2] = (mul(s[2], rhs.s[0]) + mul(s[3], rhs.s[2])) % m;
    		res.s[3] = (mul(s[2], rhs.s[1]) + mul(s[3], rhs.s[3])) % m;
    		return res;
    	}
    };
    int main() {
    	scanf("%lld%lld%lld%lld%lld%lld", &m, &a, &c, &x0, &n, &g);
    	matrix base(a, 0, 1, 1), ans(x0, c, 0, 0);
    	while (n) {
    		if (n & 1) ans = ans * base;
    		base = base * base;
    		n >>= 1;
    	}
    	printf("%lld
    ", ans.s[0] % g);
    	return 0;
    } 
    

      

  • 相关阅读:
    BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
    BZOJ 3131 SDOI2013 淘金 数位dp
    BZOJ 4408 FJOI2016 神秘数 可持久化线段树
    [leetcode] Reverse Linked List
    走迷宫问题总结
    [leetcode] Max Area of Island
    [leetcode] All Paths From Source to Target
    [leetcode] Arithmetic Slices
    [leetcode] Move Zeroes
    [leetcode] Linked List Cycle
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9491857.html
Copyright © 2011-2022 走看看