hdu 5895 Mathematician QSC(快速幂+指数循环节)

本文介绍了一种基于特殊数学序列预测学生学期成绩的方法。通过定义QSC序列并利用该序列的特性,结合学生的生日、学年初始年份、课程编号及总分等信息,实现了成绩的预测。文章还讨论了如何解决计算复杂度问题,并提供了具体的实现代码。

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

Mathematician QSC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 326    Accepted Submission(s): 172


Problem Description
QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law.

Through unremitting efforts, one day he finally found the QSC sequence, it is a very magical sequence, can be calculated by a series of calculations to predict the results of a course of a semester of a student.

This sequence is such like that, first of all, f(0)=0,f(1)=1,f(n)=f(n2)+2f(n1)(n2) Then the definition of the QSC sequence is  g(n)=ni=0f(i)2 . If we know the birthday of the student is n, the year at the beginning of the semester is y, the course number x and the course total score s, then the forecast mark is  xg(ny)%(s+1) .
QSC sequence published caused a sensation, after a number of students to find out the results of the prediction is very accurate, the shortcoming is the complex calculation. As clever as you are, can you write a program to predict the mark?
 

Input
First line is an integer T(1≤T≤1000).

The next T lines were given n, y, x, s, respectively.

n、x is 8 bits decimal integer, for example, 00001234.

y is 4 bits decimal integer, for example, 1234.
n、x、y are not negetive.

1≤s≤100000000
 

Output
For each test case the output is only one integer number ans in a line.
 

Sample Input
  
2 20160830 2016 12345678 666 20101010 2014 03030303 333
 

Sample Output
  
1 317
 

Source
 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5901 5900 5899 5898 5897 

大意就是给你一个斐波那契数列的平方加为指数的一个级数让你求这个级数%(s+1)后的值。

一开始是想用欧拉定理,先对x进行因数分解之后用欧拉定理降幂,但是没有考虑到(s+1)%因子==0的情况,

之后从学长那里知道指数循环节这个东西。。A^bmodc==A^(b%phi(c)+phi(c))%c

之后问题便迎刃而解了。

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define F(x,a,b) for (ll x=a;x<=b;x++)
#define ll long long
#define me(x) memset(x,0,sizeof(x))
#define _fast(x) F(i,1,4)F(j,1,4)F(k,1,4) b[i][j]=(b[i][j]+a[i][k]*a[k][j])%x;
#define _reset  F(i,1,4)F(j,1,4) {a[i][j]=b[i][j];b[i][j]=0;}
#define _orz(x) F(i,1,4)F(j,1,4)F(k,1,4) b[i][j]=(b[i][j]+a[i][k]*c[k][j])%x;
ll a[10][10],b[10][10],c[10][10],p[100005],isp[100005];
ll cnt;
void fastmat(ll k,ll MOD)
{
    if (k==1)return;
    if (k&1){fastmat(k-1,MOD);_orz(MOD) _reset}
    else {fastmat(k/2,MOD);_fast(MOD) _reset}
}
void _pr(ll x)
{
    F(i,2,x)
    {
        if (!p[i]) isp[++p[0]]=i;
        F(j,1,p[0])
        {
            if (isp[j]*i>x) break;
            p[isp[j]*i]=1;
            if (i%isp[j]==0) break;
        }
    }
}
ll phi(ll x)
{
    ll t=x;
    ll res=x;
    F(i,1,p[0])
    {
        if (isp[i]*isp[i]>x) break;
        if (t%isp[i]==0)
        {
            res-=res/isp[i];
            while (t%isp[i]==0) t/=isp[i];
        }
    }
    if (t>1) res-=res/t;
    return res;
}
ll _q(ll a,ll b,ll MOD)
{
    if (!b) return 1;
    if (b&1) return a*_q(a,b-1,MOD)%MOD;
    else
    {
        ll tt=_q(a,b/2,MOD)%MOD;
        return tt*tt%MOD;
    }
}
int main()
{
    ll N;
    cin>>N;
    _pr(100000);
    while (N--)
    {
        ll n,y,x,s;
        cin>>n>>y>>x>>s;
        if (n*y==0) {cout<<"1"<<endl;continue;}
        if (x==0) {cout<<"0"<<endl;continue;}
        me(a);me(b);me(c);
        a[1][1]=1;a[1][3]=1;a[2][3]=1;a[3][2]=9;a[3][3]=4;a[3][4]=4;
        a[4][2]=2;a[4][4]=1;
        F(i,1,4)F(j,1,4)c[i][j]=a[i][j];
        ll ph=phi(s+1);
        ll t=n*y;
        fastmat(t,ph);
        cout<<_q(x,a[1][3]+ph,s+1)%(s+1)<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值