uva 11582(大fib,打表找循环节)

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

Sample input

three integers a,b,n where 0 ≤ a,b < 264 (a and b will
not both be zero) and 1 ≤ n ≤ 1000.

T

a  b  n

3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample output

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

1
21
250

其实这题的主要知识,还是找循环节,打表。

卡点:2^64-1 --->   unsigned long long

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#define bug(a) cout<<a<<"--->\n";
using namespace std;

typedef unsigned long long ULL;

vector<int>f[1005];
int period[1005];

int qpow(ULL a,ULL b,int p)
{
    int ans=1;
    while(b)
    {
        if(b&1) ans=int((ans*a)%p);
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

void pre_fib()
{
    for(int n=2;n<=1000;n++)
    {
        f[n].push_back(0);f[n].push_back(1);
        for(int i=2;;i++)
        {
            f[n].push_back((f[n][i-1]+f[n][i-2])%n);
            if(f[n][i-1]==0&&f[n][i-2]==1)
            {
                period[n]=i-1;
                break;
            }
        }
    }
}

int main()
{
    int T;
    pre_fib();
    scanf("%d",&T);
    while(T--)
    {
        ULL a,b;
        int p;
        int c;
        scanf("%llu%llu%d",&a,&b,&p);
        if(a==0||p==1) printf("0\n");
        else
        {
            c=qpow(a%period[p],b,period[p]);
            printf("%d\n",f[p][c]);
        }
    }
    return 0;
}

uva 11582(大fib,打表找循环节)

时间: 2024-07-29 17:32:19

uva 11582(大fib,打表找循环节)的相关文章

特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数学知识点:特征根法求递推数列通项公式. 方法是这样的: 对于这题的解法: 记λ1=5+2√6,λ2=5-2√6,则λ1λ2=1,λ1+λ2=10 根据韦达定理可以推导出:λ1,λ2的特征方程为 x^2-10x+1=0 再使用该特征方程反向推导出递推公式为:a[n]=10*a[n-1]-a[n-2] 再由特征根

Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩溃的.(吐血......) 可是仔细观察,它指数大,可是mod小啊,它吓人,可是可以暴力搞啊!! 这个题目一个难点就是要向下取整求余,详解见传送门,本题是向下取整,也就是向上取整加一. 还有就是指数太大,要找到循环节,其实由于mod小,循环节并没有太大,暴力跑就ok啦!  此刻内心是崩溃的 1 #i

2016&quot;百度之星&quot; - 初赛(Astar Round2A)1001 All X(HDU5690)——找循环节|快速幂

一个由m个数字x组成的新数字,问其能否mod k等于c. 先提供第一种思路,找循环节.因为每次多一位数都是进行(t*10+x)mod k(这里是同余模的体现),因为x,k都确定,只要t再一样得到的答案一定一样.所以在一步一步中进行时一旦出现了一个之前出现过的数字,那么很显然后面就要开始进行循环了.找出这个循环节,然后把m放到这个循环节里头就行(这里的说法有问题,见下文的第二点). 但是这里有两个要注意的地方,第一是只有当前出现的数一样才能保证下一个出现的数一样,而并不代表着,当前的数一样,得到它

hdu 3054 Fibonacci 找循环节的公式题

Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description We know the Fibonacci Sequence F1=1,F2=1,F3=2,F4=3,F5=5, ... Fx = Fx-1+Fx-2 We want to know the Mth number which has K consecutive "0&qu

hdu 5690(同余定理找循环节 / 快速幂)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define lld I64d 5 #ifdef _WIN32 6 #define LLD "%I64d" 7 #else 8 #define LLD "%lld" 9 #endif 10 const int N = 10000 + 100; 11 ll x,m,c,k; 12 int pos[N]; 1

hud5451_求循环节加矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 找循环节解析链接:http://blog.csdn.net/ACdreamers/article/details/25616461 裸题链接:http://blog.csdn.net/chenzhenyu123456/article/details/48529039 1 #include <algorithm> 2 #i

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)

Colossal Fibonacci Numbers! 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 int

UVA 11582 Colossal Fibonacci Numbers! 找循环节

注意n=1的情况 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #inc

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes