首页 > 其他分享 >洛谷-5175

洛谷-5175

时间:2022-11-15 10:13:29浏览次数:66  
标签:洛谷 5175 int a1 a2 mod

洛谷-5175

思路

这里面的第一篇题解

根据需要啥就加啥的想法!(妙啊)

Code

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

using ll = long long;
#define int long long
const int mod = 1e9 + 7;    // 如果mod不是const,会超时
const int maxn = 2e5 + 10;
const int N = 4, M = 5010;
const int inf = 0x3f3f3f3f;

struct mat
{
    int m[N][N];
};

mat operator * (const mat&a, const mat&b) {
    mat c;
    memset(c.m,0, sizeof c.m);
    for (int i = 0;i < N;i++) {
        for (int j = 0;j < N;j++) {
            for (int k = 0;k < N;k++) {
                c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j] % mod) % mod;
            }
        }
    }
    return c;
}

mat qpow(mat a, int n) {
    mat c;
    memset(c.m, 0, sizeof c.m);
    for (int i = 0;i < N;i++) c.m[i][i] = 1;
    while(n) {
        if (n & 1) {
            c = c * a;
        }
        a = a * a;
        n >>= 1;
    }
    return c;
}

ll n, a1,a2, x, y;

inline void _A_A_() {
    #ifdef LOCAL
    freopen("in.in", "r", stdin);
    #endif
    _u_u_;
    mat s;
    cf {
        cin >> n >> a1 >> a2 >> x >> y;
        memset(s.m, 0, sizeof s.m);
        s.m[0][0] = s.m[1][0] = s.m[1][2] = 1;
        s.m[1][1] = x * x % mod, s.m[1][3] = x % mod, s.m[2][1] = y * y % mod, s.m[3][1] = 2 * x * y % mod, s.m[3][3] = y % mod;
        s = qpow(s, n - 1);
        int ans = ((((a1 * a1) % mod) * s.m[0][0] % mod) + ((a2 * a2 % mod) * s.m[1][0]) % mod + ((a1 * a1 % mod) * s.m[2][0]) % mod + ((a1 * a2%mod) * s.m[3][0] ) %mod)%mod;
        cout << ans << "\n";
    }
}

标签:洛谷,5175,int,a1,a2,mod
From: https://www.cnblogs.com/FanWQ/p/16891480.html

相关文章

  • 洛谷刷题_P1009 [NOIP1998 普及组] 阶乘之和
    题目P1009[NOIP1998普及组]阶乘之和题目链接https://www.luogu.com.cn/problem/P1009知识点求阶乘正常做法:#include<stdio.h>longlongjiecheng(longlongn)......
  • 洛谷 P1654
    设当前枚举到第\(i\)位,\(x\)为\(i\)前面期望连续\(1\)的个数。令\(a_i=x,b_i=x^2,c_i=x^3\)。\(a\)很好转移,\[a_i=(a_{i-1}+1)\timesp_i\]\(b\)的转移考虑......
  • 洛谷-2044
    洛谷-2044思路首先对与递推式\(x_n=(a*x_{n-1}+c)\)%\(mod\),发现\(c\)的存在使得不能直接使用整数快速幂求出\(x_n\),因为\(x_n\)是关于\(a,c,x0\)的\(n\)次......
  • 洛谷 P6142
    先对\(k\)进行二分,将最值问题转化成判定问题。判定一个\(k\)是否合法时,贪心去考虑,一个节点下面的若干条链在合并时,一条链肯定和另一条使它合并后恰好满足长度限制的链......
  • 洛谷-1939
    洛谷-1939思路原来我是打算按照斐波那契数列的方法,找到一个二维矩阵,因为\(a_x=a_{x-1}+a_{x-3}\),只有两个元素。但是错了,得不到正确答案。再看题解,全都是三维矩阵,......
  • 洛谷 P6147
    和这题有点类似。首先不难发现,如果当前check的值不是\(n-1\)的约数,一定无解。然后进行一遍DFS,每次用一个multiset保存子树传来的残链长度,然后贪心的配对。最后如......
  • 洛谷 P3523
    感觉和COCI2021-2022#4的T4一模一样。显然考虑二分,设当前二分出的值是\(lim\)。那么就是称一个点能覆盖另一个点当且仅当它被选中且它与那个点的距离不大于\(lim......
  • 洛谷 P1002 [NOIP2002 普及组] 过河卒
    第一个dp(动态规划)题纪念一下先尝试暴力写一个递归,由于x与y只能增加,不存在回路。#include<iostream>usingnamespacestd;inta_x,a_y,h_x,h_y,sum=0;//a_x,a_y代表目标地......
  • 洛谷P8849 『JROI-7』hibernal 二分法题解
    题目链接题目大意:交互题,给定N=2或18或64或512或1000,其中你要通过19次以内的询问在数列1-N中找到给定的未知的两个数x和y(本题解中设x<y),每次询问......
  • 洛谷-1052
    洛谷-1052思路文字版视频版Code#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullptr)#definecfint_o_o......