HDU-3221

Brute-force Algorithm

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2560    Accepted Submission(s): 657

Problem Description

Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:

Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.

Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.

Input

There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.

For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.

Output

For each test case, output the answer with case number in a single line.

Sample Input

3
3 4 10 3
4 5 13 5
3 2 19 100

Sample Output

Case #1: 2

Case #2: 11

Case #3: 12

Source

2009 Asia Shanghai Regional Contest Host by DHU

/**
    题意:根据题意可以知道求 f(n) = f(n-1)*f(n-2)的值
        f(1) = a
        f(2) = b;
        f(3) = a*b;
        f(4) = a*b^2
        f(5) = a^2*b^3
        ......
        可以得知 a,b 的指数是斐波纳锲数列
    做法:欧拉 + 矩阵 + 蒙哥马利幂模算法        ps:没有搞清楚一点  在求矩阵的n-3次幂可以过  可是n-2次幂 不能过
**/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define SIZE 2
#define clr( a, b ) memset( a, b, sizeof(a) )
long long MOD;
struct Mat
{
    long long mat[ SIZE ][ SIZE ];
    int n;
    Mat(int _n) {
        n = _n;
        clr(mat, 0);
    }
    void init() {
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j) {
                mat[i][j] = (i == j);
            }
    }
    Mat operator * (const Mat& b) const {
        Mat c(b.n);
        for(int k = 0; k < n; ++k)
            for(int i = 0; i < n; ++i) {
                if(mat[i][k])
                    for(int j = 0; j < n; ++j) {
                        c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
                    }
            }
        return c;
    }
};

Mat fast_mod(Mat a, int b)
{
    Mat res(a.n);
    res.init();
    while(b)
    {
        if(b & 1) {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

long long eular(long long n)
{
    long long ans = n;
    for(int i = 2; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            ans -= ans / i;
            while(n % i == 0) {
                n /= i;
            }
        }
    }
    if(n > 1) {
        ans -= ans / n;
    }
    return ans;
}
long long modPow(long long s, long long index, long long mod)
{
    long long ans = 1;
    s %= mod;
    while(index >= 1)
    {
        if((index & 1) == 1) { //奇数
            ans = (ans * s) % mod;
        }
        index >>= 1;
        s = s * s % mod;
    }
    return ans;
}
int main()
{
    int T;
    int Case = 1;
    scanf("%d", &T);
    while(T--)
    {
        long long x, y, n, res, p;
        cin >> x >> y >> p >> n;
        printf("Case #%d: ", Case++);
        if(p == 0 || p == 1) {
            printf("0\n");
            continue;
        }
        if(n == 1)
        {
            cout << x % p << endl;
            continue;
        }
        else if(n == 2)
        {
            cout << y % p << endl;
            continue;
        }
        MOD = eular(p);
        Mat C(2);
        C.mat[0][0] = 0;
        C.mat[0][1] = 1;
        C.mat[1][0] = 1;
        C.mat[1][1] = 1;
        C = fast_mod(C, n - 3);
        long long aa =  C.mat[0][0] + C.mat[0][1];
        long long  bb = C.mat[1][1] + C.mat[1][0];
        res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p;
        cout << res << endl;
    }
    return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define SIZE 2
#define clr( a, b ) memset( a, b, sizeof(a) )
long long MOD;
struct Mat
{
    long long mat[ SIZE ][ SIZE ];
    int n;
    Mat(int _n) {
        n = _n;
        clr(mat, 0);
    }
    void init() {
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j) {
                mat[i][j] = (i == j);
            }
    }
    Mat operator * (const Mat& b) const {
        Mat c(b.n);
        for(int k = 0; k < n; ++k)
            for(int i = 0; i < n; ++i) {
                if(mat[i][k])
                    for(int j = 0; j < n; ++j) {
                        c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
                    }
            }
        return c;
    }
};

Mat fast_mod(Mat a, int b)
{
    Mat res(a.n);
    res.init();
    while(b)
    {
        if(b & 1) {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

long long eular(long long n)
{
    long long ans = n;
    for(int i = 2; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            ans -= ans / i;
            while(n % i == 0) {
                n /= i;
            }
        }
    }
    if(n > 1) {
        ans -= ans / n;
    }
    return ans;
}
long long modPow(long long s, long long index, long long mod)
{
    long long ans = 1;
    s %= mod;
    while(index >= 1)
    {
        if((index & 1) == 1) { //奇数
            ans = (ans * s) % mod;
        }
        index >>= 1;
        s = s * s % mod;
    }
    return ans;
}
int main()
{
    int T;
    int Case = 1;
    scanf("%d", &T);
    while(T--)
    {
        long long x, y, n, res, p;
        cin >> x >> y >> p >> n;
        printf("Case #%d: ", Case++);
        if(p == 0 || p == 1) {
            printf("0\n");
            continue;
        }
        if(n == 1)
        {
            cout << x % p << endl;
            continue;
        }
        else if(n == 2)
        {
            cout << y % p << endl;
            continue;
        }
        MOD = eular(p);
        Mat C(2);
        C.mat[0][0] = 1;
        C.mat[0][1] = 1;
        C.mat[1][0] = 1;
        C.mat[1][1] = 0;
        C = fast_mod(C, n - 3);
        long long aa = C.mat[1][0] + C.mat[1][1];
        long long  bb = C.mat[0][0] + C.mat[0][1];
        res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p;
        cout << res << endl;
    }
    return 0;
}
时间: 2024-11-05 12:22:03

HDU-3221的相关文章

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列

HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1]*f[n-2].然后a和b系数都是呈斐波那契规律增长的.需要先保存下来指数.但是太大了.在这里不能用小费马定理.要用降幂公式取模.(A^x)%C=A^(x%phi(C)+phi(C))%C(x>=phi(C)) Phi[C]表示不大于C的数中与C互质的数的个数,可以用欧拉函数来求. 矩阵快速幂也不

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> #incl

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

(记忆化搜索) FatMouse and Cheese(hdu 1078)

题目大意: 给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值 (题目很容易会理解错题意,道友小心) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std;

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是