一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节。数据范围出的很那啥..unsigned long long注意用防爆的乘法

/** @Date    : 2017-09-25 15:17:05
* @FileName: HDU 2814 斐波那契循环节 欧拉降幂.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL unsigned long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const double eps = 1e-8; LL mul(LL a, LL b, LL m) {
LL ans = 0;
while (b) {
if (b & 1) {
ans = (ans + a) % m;
b--;
}
b >>= 1;
a = (a + a) % m;
}
return ans;
}
/* RERERERERE 精度
LL mul(LL x, LL y, LL mod)
{
return (x * y - (LL)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}*/
/*
struct Matrix
{
LL m[M][M];
}; Matrix A;
Matrix I = {1, 0, 0, 1}; Matrix multi(Matrix a, Matrix b, LL MOD)
{
Matrix c;
for(int i = 0; i < M; i++)
{
for(int j = 0; j < M; j++)
{
c.m[i][j] = 0;
for(int k = 0; k < M; k++)
c.m[i][j] = (c.m[i][j] % MOD + (a.m[i][k] % MOD) * (b.m[k][j] % MOD) % MOD) % MOD;
c.m[i][j] %= MOD;
}
}
return c;
} Matrix power(Matrix a, LL k, LL MOD)
{
Matrix ans = I, p = a;
while(k)
{
if(k & 1)
{
ans = multi(ans, p, MOD);
k--;
}
k >>= 1;
p = multi(p, p, MOD);
}
return ans;
} LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
} const int N = 400005;
const int NN = 5005; LL num[NN], pri[NN];
LL fac[NN];
int cnt, c; bool prime[N];
int p[N];
int k; void isprime()
{
k = 0;
memset(prime, true, sizeof(prime));
for(int i = 2; i < N; i++)
{
if(prime[i])
{
p[k++] = i;
for(int j = i + i; j < N; j += i)
prime[j] = false;
}
}
} LL fpow(LL a, LL b, LL m)
{
LL ans = 1;
a %= m;
while(b)
{
if(b & 1)
{
ans = mul(ans, a , m);
b--;
}
b >>= 1;
a = mul(a , a , m);
}
return ans;
} LL legendre(LL a, LL p)
{
if(fpow(a, (p - 1) >> 1, p) == 1)
return 1;
else return -1;
} void Solve(LL n, LL pri[], LL num[])
{
cnt = 0;
LL t = (LL)sqrt(1.0 * n);
for(int i = 0; p[i] <= t; i++)
{
if(n % p[i] == 0)
{
int a = 0;
pri[cnt] = p[i];
while(n % p[i] == 0)
{
a++;
n /= p[i];
}
num[cnt] = a;
cnt++;
}
}
if(n > 1)
{
pri[cnt] = n;
num[cnt] = 1;
cnt++;
}
} void Work(LL n)
{
c = 0;
LL t = (LL)sqrt(1.0 * n);
for(int i = 1; i <= t; i++)
{
if(n % i == 0)
{
if(i * i == n) fac[c++] = i;
else
{
fac[c++] = i;
fac[c++] = n / i;
}
}
}
} LL get_loop(LL n)
{
Solve(n, pri, num);
LL ans = 1;
for(int i = 0; i < cnt; i++)
{
LL record = 1;
if(pri[i] == 2)
record = 3;
else if(pri[i] == 3)
record = 8;
else if(pri[i] == 5)
record = 20;
else
{
if(legendre(5, pri[i]) == 1)
Work(pri[i] - 1);
else
Work(2 * (pri[i] + 1));
sort(fac, fac + c);
for(int k = 0; k < c; k++)
{
Matrix a = power(A, fac[k] - 1, pri[i]);
LL x = (a.m[0][0] % pri[i] + a.m[0][1] % pri[i]) % pri[i];
LL y = (a.m[1][0] % pri[i] + a.m[1][1] % pri[i]) % pri[i];
if(x == 1 && y == 0)
{
record = fac[k];
break;
}
}
}
for(int k = 1; k < num[i]; k++)
record *= pri[i];
ans = ans / gcd(ans, record) * record;
}
return ans;
} LL fib[5005]; void Init()
{
A.m[0][0] = 1;
A.m[0][1] = 1;
A.m[1][0] = 1;
A.m[1][1] = 0;
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i < 5005; i++)
fib[i] = fib[i - 1] + fib[i - 2];
}*///会爆ULL C比较小300 不如直接求
LL fib[5500]; LL get_loop(LL n)
{
LL lp = -1;
fib[0] = 0, fib[1] = 1;
for(int i = 2; i < 2010; i++)
{
fib[i] = (fib[i - 1] % n + fib[i - 2] % n) % n;
if(fib[i] == 1 && fib[i - 1] == 0)
return lp = i - 1;
}
return lp;
}
LL fpow(LL a, LL n, LL mod)
{
LL res = 1;
a %= mod;
while(n)
{
if(n & 1)
res = mul(res, a, mod);
a = mul(a, a, mod);
n >>= 1;
}
return res;
} int get_phi(int x)
{
int ans = x;
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
while (x % i == 0)
x /= i;
ans = ans - ans / i;
}
}
if (x > 1)
ans = ans - ans / x;
return ans;
} int main()
{
LL n;
//Init();
//isprime();
int T;
cin >> T;
int icas = 0;
while(T--)
{
LL a, b, n, C;
scanf("%llu%llu%llu%llu", &a, &b, &n, &C);
if(C == 1)
{
printf("Case %d: 0\n", ++icas);
continue;
}
int lp1 = get_loop(C);
LL e1 = fpow(a, b, lp1);
LL ans1 = fib[e1] % C; LL phi = get_phi(C);
int lp2 = get_loop(phi);
LL e2 = fpow(a, b, lp2);
LL ans2 = fpow(fib[e2] % phi, n - 1, phi) + phi; LL ans = fpow(ans1, ans2, C);
printf("Case %d: %llu\n", ++icas, ans); }
return 0;
}

HDU 2814 斐波那契循环节 欧拉降幂的更多相关文章

  1. 2019南昌网络赛H The Nth Item(二阶线性数列递推 + 广义斐波那契循环节 + 分段打表)题解

    题意: 传送门 已知\(F(n)=3F(n-1)+2F(n-2) \mod 998244353,F(0)=0,F(1)=1\),给出初始的\(n_1\)和询问次数\(q\),设每一次的答案\(a_i= ...

  2. 2019牛客多校第五场 generator 1——广义斐波那契循环节&&矩阵快速幂

    理论部分 二次剩余 在数论中,整数 $X$ 对整数 $p$ 的二次剩余是指 $X^2$ 除以 $p$ 的余数. 当存在某个 $X$,使得式子 $X^2 \equiv d(mod \ p)$ 成立时,称 ...

  3. 指数循环节&欧拉降幂

    证明:https://www.cnblogs.com/maijing/p/5046628.html 注意使用条件(B的范围) 例题: FZU1759 HDU2837 ZOJ1674 HDU4335

  4. HDU 3977 斐波那契循环节

    这类型的题目其实没什么意思..知道怎么做后,就有固定套路了..而且感觉这东西要出的很难的话,有这种方法解常数会比较大吧..所以一般最多套一些比较简单的直接可以暴力求循环节的题目了.. /** @Dat ...

  5. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  6. HDU 1021(斐波那契数与因子3 **)

    题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...

  7. HDU 2516 斐波那契博弈

    点这里去看题 n为斐波那契数时,先手败,推断方法见算法讲堂 #include<bits/stdc++.h> using namespace std; int main() { ],i,n, ...

  8. HDU——4549M斐波那契数列(矩阵快速幂+快速幂+费马小定理)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  9. HDU 5686 斐波那契数列、Java求大数

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...

随机推荐

  1. FindBugs插件的使用手册

    安装FindBugs直接查找eclipse的商店,查找spot Bugs 插件,安装即可 完成安装之后重启eclipse,右击项目文件或目录,会发现多了Findbugs的菜单: 使用Findbugs ...

  2. vue-cli 安装时 npm 报错 errno -4048

    如何解决vue-cli 安装时  npm 报错 errno -4048 第一种解决方法:以管理身份运行cmd.exe 第二种解决办法:在dos窗口输入命令  npm cache clean  --fo ...

  3. PAT 1147 Heaps

    https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, a ...

  4. SEO优化之HTML代码优化最重要的5个标签

    众所周知,HTML代码一直是搜索引擎抓取的重点.搜索引擎会将HTML中的某些元素标签作为影响网页排名的重要依据 在我们之前的文章中也或多或少地向大家介绍了有关HTML代码的优化技巧,接下来将系统地讲解 ...

  5. 微信小程序入门一: 简易form、本地存储

    实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...

  6. redis哨兵机制二(转)

    概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如 master宕机了,Redis本身(包括它的很多客户端) ...

  7. Oracle 11G RAC For Windows 2008 R2部署手册(亲测,成功实施多次)

    总体规划 服务器规划 1.建议使用两台硬件配置一模一样的服务器来作为 RAC 环境的两个物理节点 2.服务器至少需要配置两块物理网卡 3.服务器规划表: 节点 主机名 本地磁盘大小 操作系统 内存大小 ...

  8. SQLSERVER 升级版本的方法

    1. 以SQLSERVER2014为例说明 SQLSERVER升级版本的方法, 也适用于evaluation 版本超过180天之后的处理. 2. 打开所有的应用 看到有一个 sqlserver2008 ...

  9. [OS] 内核态和用户态的区别

    http://blog.csdn.net/fatsandwich/article/details/2131707# http://jakielong.iteye.com/blog/771663 当一个 ...

  10. sql中的duplicate的使用

    应用场景:有时候在做一些系统设置功能的时候,系统在第一次使用,或者初始化的时候,该设置信息并没有存在于数据库中,或者该系统设置信息永远只保存一条,没有必要为增加和修改这条信息而分别编写insert和u ...