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


    题目链接:https://vjudge.net/contest/156903#problem/A

    题意:计算 f(a^b)%n

    分析:

    1、斐波那契数列是 f(i+2) = f(i+1) + f(i)

    2、询问次数是10^4,打表处理;设 f(n,i) 是 f(i) %n 的余数;

    3、根据模运算可以知道:f(n,i) = ( f(n,i-1) + f(n,i-2) ) % n;

    4、 a^b的处理了,a,b<2^64,数据很大,但是可以发现一个特征,n很小;取值范围很小;可以看其周期性;

    5、a^b 对 %n 的周期,的快速幂取模了;

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 const int maxn = 1000 + 5;
     6 typedef unsigned long long ULL;
     7 
     8 int pow_mod(ULL a,ULL b,int n) {
     9     if(b==0) return 1;
    10     int k = pow_mod(a,b/2,n);
    11     k = k*k%n;
    12     if(b%2) k = k*a%n;
    13     return k;
    14 }
    15 
    16 int f[maxn][maxn*6],period[maxn];
    17 
    18 int solve(ULL a,ULL b,int n) {
    19     if(a==0||n==1) return 0;
    20     int p = pow_mod(a%period[n],b,period[n]);
    21     return f[n][p];
    22 }
    23 
    24 int main()
    25 {
    26     //f(n,i) %n 时 i 的余数
    27     for(int n=2;n<=1000;n++) {
    28         f[n][0] =0; f[n][1] = 1;
    29         for(int i=2;;i++) {
    30             f[n][i] = (f[n][i-1]+f[n][i-2]) % n;
    31             if(f[n][i-1]==0&&f[n][i]==1)
    32             {
    33                 period[n] = i-1;
    34                 break;
    35             }
    36         }
    37     }
    38 
    39     ULL a,b;
    40     int n,t;
    41     scanf("%d",&t);
    42     while(t--) {
    43         cin>>a>>b>>n;
    44         cout<<solve(a,b,n)<<endl;
    45     }
    46 
    47 
    48 
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    MFC 控件RadioButton和CheckBox区别
    python的传递实参
    python的返回值
    Machine Learning的定义
    pythion的定义函数和传递实参
    python的用户输入和while循环
    python的字典
    python的if语句
    python的元组及其书写规矩
    python中操作列表
  • 原文地址:https://www.cnblogs.com/TreeDream/p/6659300.html
Copyright © 2020-2023  润新知