https://vjudge.net/problem/UVA-11582

题意:

输入两个非负整数a、b和正整数n,你的任务是计算f(a^b)除以n的余数。f[0]=0,f[1]=1,f[i+2]=f[i+1]+f[i]。

思路:

因为余数最多n种,所以最多n^2项就会出现重复。计算出周期,之后幂取模算出周期内的第几个数。

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int maxn = + ;
typedef unsigned long long LL; LL a, b;
int n;
int M; int pow_mod(LL a, LL b, int m)
{
if (b == ) return ;
int x = pow_mod(a, b / , m);
LL ans = (LL)x*x%m;
if (b % ) ans = ans*a%m;
return (int)ans;
} int f[maxn*maxn], period[maxn]; int solve(LL a, LL b, int n) {
if (a == || n == ) return ;
int ans = pow_mod(a%M, b, M);
return f[ans];
} int main()
{
//freopen("D:\\input.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
cin >> a >> b >> n;
f[] = ;
f[] = ;
for (int i = ; i <= n*n; i++)
{
f[i] = (f[i - ] + f[i - ]) % n;
if (f[i - ] == && f[i] == )
{
M = i - ;
break;
}
}
cout << solve(a, b, n) << endl;
}
}

UVa 11582 巨大的斐波那契数!(幂取模)的更多相关文章

  1. Uva 11582 巨大的斐波那契数 模运算

    题目链接:https://vjudge.net/contest/156903#problem/A 题意:计算 f(a^b)%n 分析: 1.斐波那契数列是 f(i+2) = f(i+1) + f(i) ...

  2. 2018年东北农业大学春季校赛 K wyh的数列【数论/斐波那契数列大数取模/循环节】

    链接:https://www.nowcoder.com/acm/contest/93/K来源:牛客网 题目描述 wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F( ...

  3. UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数

    大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...

  4. 斐波那契数[XDU1049]

    Problem 1049 - 斐波那契数 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 1673  Ac ...

  5. C++求斐波那契数

    题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...

  6. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  7. DP:斐波纳契数

    题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...

  8. HDU4549 M斐波那契数

    M斐波那契数列 题目分析: M斐波那契数列F[n]是一种整数数列,它的定义例如以下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 如今给 ...

  9. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

随机推荐

  1. Express 框架的安装

    从零开始用 Node.js 实现一个微博系统,功能包括路由控制.页面模板.数据库访问.用户注册.登录.用户会话等内容. Express 框架. MVC 设计模式. ejs 模板引擎 MongoDB 数 ...

  2. 【BZOJ4099】Trapped in the Haybales Gold STL

    [BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...

  3. hibernate在Oracle中插入数据,默认字段被设置为null的问题解决

    参考内容: http://blog.sina.cn/dpool/blog/s/blog_90629d5301014a5w.html 在数据库中一个字段的默认值为1,但是在插入数据后,本来该字段为空,值 ...

  4. 几种常用的SQL优化工具及方法

    转自:http://blog.itpub.net/35489/viewspace-764856/ 1. sql 详细执行计划,主要检查驱动路径,索引是否合适:同一个pl/sql窗口连续执行即可:exp ...

  5. 阅读笔记:Solving the “false positives” problem in fraud prediction

    刚读完一篇paper<Solving the “false positives” problem in fraud prediction>,趁热打铁,做个笔记. 文章下载链接:https: ...

  6. 170609、Nginx配置文件详细说明

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processe ...

  7. 项目无法运行iPhone5模拟器

    公司没有iPhone5真机, 有人反馈iPhone5有bug, 只能用模拟器验证bug, 但是使用iPhone5项目编译不过,报错: 注释掉相关引用代码,去掉 库

  8. Django - rest - framework - 下

    一.视图三部曲 https://www.cnblogs.com/wupeiqi/articles/7805382.html 使用混合(mixins) 之前得视图部分 # urls.py from dj ...

  9. leetcode之Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  10. Oil Skimming---hdu4185(最大匹配)

    题目链接 题意:有一个地图.代表水#代表油每个单元格是10*10的,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油: 每次提取的时候勺子放的位置都要是油,不然就被污染而没有价值了: ...