【ACMICPC Chengdu Online】1004 数论,类Fib

本文讨论了如何通过观察序列的周期性规律来优化计算复杂递归序列的值,具体介绍了暴力求解和正推求解的方法,并提供了解决这类问题的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

公式g[n] = 3 * g[n-1] + g[n-2]

          求: g(g(g(n))) % (10^9 + 7)



解法:

  1、暴力求 g(n) % (10 ^ 9 + 7) 的循环节  —— 222222224LL

  2、暴力求 g(n) % (222222224LL) 的循环节 —— 183120LL

  3、正推, 求出答案。


代码:

/*
 * =====================================================================================
 *
 *         Author:  KissBuaa.DS(AC)
 *        Company:  BUAA-ACMICPC-Group
 *
 * =====================================================================================
 */
/*
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
const LL modo = 222222224LL; //打表
LL a[3];
int main(){
    freopen("mengzhu.txt","w",stdout);
    a[0] = 0LL , a[1] = 1LL;
    LL i = 2LL;
    for( ; ; i= i + 1LL){
        a[ i % 3 ] = (a[ (i + 1) % 3] + 3LL * a[ (i + 2 ) % 3])%modo;
        if (a[ i % 3 ] == 1 && a[ (i+2) % 3] ==0) break;
    }
    cout<< i << endl;
}

222222224LL
183120LL
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include <math.h>
#define LL __int64
#define CLR(x) memset(x,0,sizeof(x))
#define typec LL
#define sqr(x) ((x)*(x))
#define abs(x) ((x)<0?(-(x)):(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define inf 100000000
//For C++
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <iostream>
using namespace std;
const double eps=1e-10;
int dblcmp(typec d) {
    if (fabs(d)<eps)
       return 0;
    return (d>0)?1:-1;
}
LL modo;
const int maxn=3;
class Mat
{
public:
       LL Matn,Matm;
       typec a[maxn][maxn];
       Mat()
       {
            Matn=0;
            Matm=0;
            memset(a,0LL,sizeof(a));
       }
       void output();
       void init();
       void initI();
       Mat mul(const Mat &a);
       Mat power(const Mat&a,LL k);
};
void Mat::output()
{
     for (int i=0;i<Matn;++i)
     {
         for (int j=0;j<Matm;++j)
         {
             if (j!=0) printf(" ");
             printf("%I64d",a[i][j]);
         }
         printf("\n");
     }
}
void Mat::init()
{
    Matn=0;
    Matm=0;
    memset(a,0LL,sizeof(a));
}
Mat Mat::mul(const Mat &A)
{
    Mat c;
    c.init();
    c.Matn=Matn;
    c.Matm=A.Matm;
    for (int i=0;i<Matn;++i)
        for (int j=0;j<A.Matm;++j)
        {
            for (int k=0;k<Matm;++k)
            {
                c.a[i][j]=(c.a[i][j]+(a[i][k]*A.a[k][j])%modo)%modo;
            }
        }
    return c;
}
void Mat::initI()
{
     memset(a,0LL,sizeof(a));
     for (int i=0;i<Matn;++i) a[i][i]=1LL;
}
Mat Mat::power(const Mat& a,LL  k)
{
    Mat c=a,b;
    b.init();
    b.Matn=a.Matn;b.Matm=a.Matm;
    b.initI();
    while (k)
    {
         if (k & (1LL))
            b=b.mul(c);
         c=c.mul(c);
         k>>=1LL;
    }
    return b;
}
Mat  A ,B;
LL g(LL x){
    //Mat A;
    if (x == 0LL) return 0LL;
    if (x == 1LL) return 1LL;
    //A.init();
    //A.Matn = 1LL , A.Matm = 2LL;
    //A.a[0][0]=1LL,A.a[0][1]=0LL;
    //Mat B;
    B.init();
    B.Matn = 2LL , B.Matm = 2LL;
    B.a[0][0]= 3LL, B.a[1][0]=1LL;
    B.a[0][1] = 1LL; B.a[1][1]=0LL;
    B = B.power(B , x - 1LL);
    //A = A.mul(B);
    return B.a[0][0];
}
LL n;
void solve(){
    //printf("%I64d\n",g(42837));
    modo = 183120LL;
    LL res = g(n);
    //res = (res * inv(res , modo))% modo;
    //cout<<res<<endl;
    modo = 222222224LL;
    res = g(res);
    //cout<<res<<endl;
    //res = (res * inv(res , modo))% modo;
    modo =(1000000000LL + 7LL);
    res = g(res);
    //res = (res * inv(res-1LL , modo))% modo;
    //printf("%I64d\n",g(g(g(n))));
    printf("%I64d\n",res);
}
/*0
0
set<LL> has;
void solve(){
    has.clear();
    LL i;
    for (i = 1 ; ; ++i){
        LL res = g(i);
        if(has.find(res) != has.end()){
            cout<<i<<endl;
            return;
        }
        has.insert(res);
    }
}*/
int main(){
    //solve();
    while (~scanf("%I64d",&n)) solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值