Brute-force Algorithm HDU - 3221(指数循环节 矩阵快速幂)

水题一道

推一下就是

f[n] = f[n - 1] + f[n - 2]

发现n很大

所以用矩阵快速幂就好了

还有P很大  那就指数循环节

一定要注意   这个条件

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1100000, INF = 0x7fffffff;

int prime[maxn+10], phi[maxn+10];
bool vis[maxn+10];
int ans;
LL P;
void get_phi()
{
    ans = 0;
    phi[1] = 1;
    for(int i=2; i<=maxn; i++)
    {
        if(!vis[i])
        {
            prime[++ans] = i;
            phi[i] = i - 1;
        }
        for(int j=1; j<=ans; j++)
        {
            if(i * prime[j] > maxn) break;
            vis[i * prime[j]] = 1;
            if(i % prime[j] == 0)
            {

                phi[i * prime[j]] = phi[i] * prime[j]; break;
            }
            else
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        }
    }
}

LL q_pow(LL a, LL b)
{
    LL res = 1;
    while(b)
    {
        if(b & 1) res = res * a % P;
        a = a * a % P;
        b >>= 1;
    }
    return res;
}
int tot;
struct Matrix
{
    LL v[110][110];
    Matrix()
    {
        memset(v, 0, sizeof(v));
    }
    Matrix operator *(const Matrix B)    // 重载的速度比写独立的函数慢点。
    {
        int i, j, k;
        Matrix C;
        for(i = 0; i <= tot; i ++)
            for(j = 0; j <= tot; j ++)
                for(k = 0; k <= tot; k ++)
                {
                    C.v[i][j] = (C.v[i][j] + v[i][k] * B.v[k][j]) % phi[P];

                }
        return C;
    }
};

Matrix mtPow(Matrix A, LL k)           // 用位运算代替递归求 A^k。
{
    int i;
    Matrix B;
    for(i = 0; i <= tot; i ++)
    {
        B.v[i][i] = 1;
    }
    while(k)
    {
        if(k & 1) B = B * A;
        A = A * A;
        k >>= 1;
    }
    return B;
}

int main()
{
    get_phi();
    int kase = 0;

    int T;
    rd(T);
    while(T--)
    {
        tot = 1;
        LL a, b, n;
        cin >> a >> b >> P >> n;
        if(n == 1)
            printf("Case #%d: %lld\n", ++kase, a % P);
        else if(n == 2)
            printf("Case #%d: %lld\n", ++kase, b % P);
        else if(n == 3)
            printf("Case #%d: %lld\n", ++kase, a * b % P);
        else
        {
            Matrix A, B;
            A.v[0][0] = A.v[0][1] = A.v[1][0] = 1;
            A.v[1][1] = 0;
            B = mtPow(A, n - 3);
            LL d1 = B.v[0][0] + B.v[0][1], d2 = B.v[1][0] + B.v[1][1];
            if(d2 >= phi[P]) d2 = d2 % phi[P] + phi[P];
            if(d1 >= phi[P]) d1 = d1 % phi[P] + phi[P];
            a = q_pow(a, d2) % P;
            b = q_pow(b, d1) % P;
            printf("Case #%d: ", ++kase);
            cout << a * b % P << endl;
        }

    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/10806701.html

时间: 2024-10-10 14:05:48

Brute-force Algorithm HDU - 3221(指数循环节 矩阵快速幂)的相关文章

hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law. Through unremitting e

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

HDU - 1588 Gauss Fibonacci (矩阵快速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N: 矩阵C = A*B 矩阵M=CN?N 将矩阵M中的所有元素取模6,得到新矩阵M' 计算矩阵M'中所有元素的和 解题思路:因为矩阵C为N*N的矩阵,N最大为1000,就算用快速幂也超时,但是因为C = A*B, 所以CN?N=ABAB-AB=AC′N?N?1B,C' = B*A, 为K*K的矩阵,K最大为6,完全可以接受. #include <cstdio> #inc

HDU 2294 Pendant (DP+矩阵快速幂降维)

HDU 2294 Pendant (DP+矩阵快速幂降维) ACM 题目地址:HDU 2294 Pendant 题意: 土豪给妹子做首饰,他有K种珍珠,每种N个,为了炫富,他每种珍珠都要用上.问他能做几种长度[1,N]的首饰. 分析: 1 ≤ N ≤ 1,000,000,000简直可怕. 首先想dp,很明显可以想到: dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j](dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个) 然后遇到N太大的话,

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

hdu 1588 Gauss Fibonacci(矩阵快速幂)

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2090    Accepted Submission(s): 903 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

HDU——4291A Short problem(矩阵快速幂+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2461    Accepted Submission(s): 864 Problem Description According to a research, VIM users tend to have shorter fingers, compared