巨大的斐波那契数

The i'th Fibonacci number f (i) is recursively defined in the following way:

  • f (0) = 0 and f (1) = 1
  • f (i+2) = f (i+1) + f (i)  for every i ≥ 0

Your task is to compute some values of this sequence.

Input begins with an integer t ≤ 10,000, the number of test cases. Each test case consists of three integers a,b,n where 0 ≤ a,b < 264 (a andb will not both be zero) and 1 ≤ n ≤ 1000.

For each test case, output a single line containing the remainder of f (ab) upon division by n.

Sample input

3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample output

1
21
250 题意:
输 入两个非负整数a、b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,
其中f(0) = 0,f(1) = 1;且对任意非负整数i,f(i+2)= f(i+1)+f(i)。 分析:
因为斐波那契序列要对n取模,余数只有n种,所以最多n^2项序列就开始重复,所以问题转化成了求周期然后大整数取模。
小于2^64的数要用unsigned long long。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=+;
int m[maxn],n;
typedef unsigned long long ULL;
unsigned long long a,b;
unsigned long long f[maxn][];
int pow_mod(ULL a,ULL b,int n)
{
if(b==) return ;
int x=pow_mod(a,b/,n);
unsigned long long ans=(unsigned long long )x*x%n;
if(b%==) ans=ans*a%n;
return (int)ans;
}
int main()
{
for(n=;n<=;n++)
{
f[n][]=,f[n][]=;
for(int i=;;i++)
{
f[n][i]=(f[n][i-]+f[n][i-])%n;
if(f[n][i]==&&f[n][i-]==)
{
m[n]=i-;
break;
}
}
}
int t;
scanf("%d",&t);
while(t--)
{
scanf("%llu%llu%d",&a,&b,&n);
if(n==||a==) printf("0\n");
else printf("%d\n",f[n][pow_mod(a%m[n],b,m[n])]);
}
return ;
}

UVa #11582 Colossal Fibonacci Numbers!的更多相关文章

  1. UVA 11582 Colossal Fibonacci Numbers(数学)

    Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...

  2. UVa 11582 Colossal Fibonacci Numbers! 紫书

    思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161  的代码: #include <cstdio> # ...

  3. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

  4. UVa 11582 - Colossal Fibonacci Numbers!(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. 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]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...

  6. UVA 11582 Colossal Fibonacci Numbers!【数学】

    大一刚开始接触ACM就买了<算法竞赛入门经典>这本书,当时只能看懂前几章,而且题目也没做,粗鄙地以为这本书不适合自己.等到现在快大三了再回过头来看,发现刘老师还是很棒的! 扯远了... 题 ...

  7. UVA 11582 Colossal Fibonacci Numbers!(循环节打表+幂取模)

    题目链接:https://cn.vjudge.net/problem/UVA-11582 /* 问题 输入a,b,n(0<a,b<2^64(a and bwill not both be ...

  8. UVA - 11582 Colossal Fibonacci Numbers! (巨大的斐波那契数!)

    题意:输入两个非负整数a.b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负 ...

  9. Colossal Fibonacci Numbers! UVA 11582 寻找循环节

    /** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...

随机推荐

  1. Coursera-Getting and Cleaning Data-week1-课程笔记

    博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html -- Sunday, January 11, 2015 课程概述 G ...

  2. MySQL 5.5开启慢查询功能

    vim /etc/my.cnf [mysqld] slow-query-log = on # 开启慢查询功能 slow_query_log_file = /usr/local/mysql/data/s ...

  3. 第2月第1天 GCDAsyncSocket dispatch_source_set_event_handler

    一.GCDAsyncSocket的核心就是dispatch_source_set_event_handler 1.accpet回调 accept4Source = dispatch_source_cr ...

  4. Daily Scrum Meeting ——ThirdDay

    一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.完成了github上的文档整理 Transcend/ActivityHelper 2.主界面侧滑框的 ...

  5. jquery.validate.js插件使用

    jQuery验证控件jquery.validate.js使用说明+中文API 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

  6. 获取url传参

    function urlparameterforkey(name) { //读取html 数据 ); //待处理的字符串 var patt = new RegExp(name); //要查找的字符串 ...

  7. mpp文件转换成jpg图片,可以用pdf文件做中转站

    用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了

  8. webstorm抽取函数

    webstrom 1.extact 抽取函数:选中代码,右键,refactor-extact function matchPicLink() { var $match = $('#match'); v ...

  9. C Primer Plus_第9章_函数_编程练习

    1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) ...

  10. iOS键盘输入屏幕上移

    在iOS开法中经常会遇到键盘遮挡屏幕的事情(比如输入账号密码验证码等等),就使得原本都不大的屏幕直接占了一半甚至更多的位置,这倒无所谓,关键是挡住了下面的按钮.这样的话按钮的事件也就触发不了,最好的解 ...