zoj 3707 Calculate Prime S

fibonacci数列的性质:

1.gcd(fib(n),fib(m))=fib(gcd(n,m))

证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m时gcd(fib(n),fib(m))=gcd(fib(n-m),fib(m)),递归可

求gcd(fib(n),fib(m))=gcd(fib(k),fib(l)),最后k=l,不然继续递归。K是通过展转相减法求出,易证k=gcd(n,m),所以gcd(fib(n),fib(m))

=fib(gcd(n,m))。

 

2.如果fib(k)能被x整除,则fib(k*i)都可以被x整除。

3.f(0)+f(1)+f(2)+…+f(n)=f(n+2)-1

4.f(1)+f(3)+f(5)+…+f(2n-1)=f(2n)

5.f(2)+f(4)+f(6)+…+f(2n) =f(2n+1)-1

6.[f(0)]^2+[f(1)]^2+…+[f(n)]^2=f(n)·f(n+1)

7.f(0)-f(1)+f(2)-…+(-1)^n·f(n)=(-1)^n·[f(n+1)-f(n)]+1

8.f(n+m)=f(n+1)·f(m)+f(n)*f(m-1)

9.[f(n)]^2=(-1)^(n-1)+f(n-1)·f(n+1)

10.f(2n-1)=[f(n)]^2-[f(n-2)]^2

11.3f(n)=f(n+2)+f(n-2)

12.f(2n-2m-2)[f(2n)+f(2n+2)]=f(2m+2)+f(4n-2m) [ n〉m≥-1,且n≥1]

 

还有一个结论:

计算(a/b)%c  其中b能整除a

如果b与c互素,则(a/b)%c=a*b^(phi(c)-1)%c

如果b与c不互素,则(a/b)%c=(a%bc)/b

对于b与c互素和不互素都有(a/b)%c=(a%bc)/b成立

附上代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

#define  LL long long 

struct matrix{
	LL f[2][2];
};

const int N=16000000;
int prime[16000000];
LL p[13000011];

void init(){
	int i,j;
	int k=1;
	memset(prime,0,sizeof(prime));
	p[0]=1;
	k=1;
	for(i=2;i<=N;i++){
        if(prime[i]==0){
            p[k++]=i;
            for(j=2*i;j<=N;j+=i){
                prime[j]=1;
            }
        }
	}

	p[1]=3;p[2]=4;
}

matrix multi(matrix a,matrix b,int MOD)
{
    struct matrix c;
    int i,j,k;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            c.f[i][j]=0;
            for(k=0;k<2;k++)
            {
                c.f[i][j]=(c.f[i][j]+(a.f[i][k]*b.f[k][j])%MOD)%MOD;

            }
        }
    }
    return c;
}

matrix quick_pow(int k,int MOD)
{
	int i;
    struct matrix s;
    memset(s.f,0,sizeof(s.f));
    for(i=0;i<2;i++) s.f[i][i]=1;
    struct matrix a;
    a.f[0][0]=1;a.f[0][1]=1;a.f[1][0]=1;a.f[1][1]=0;
    while(k){
        if(k&1){
            s=multi(s,a,MOD);
        }
        a=multi(a,a,MOD);
        k=k>>1;
    }
    return s;
}

int main(){
       int i,k,x,m;
       int t;
       init();
       scanf("%d",&t);
       struct matrix ans;
       while(t--){
           scanf("%d%d%d",&k,&x,&m);
            for(i=p[k];;i++){
                ans = quick_pow(i-1,x);
                if(ans.f[0][0]%x==0) break;
            }
			ans = quick_pow(i-1,x*m);
		    printf("%lld\n",ans.f[0][0]/x);
       }
        return 0;
}
时间: 2024-12-24 23:59:44

zoj 3707 Calculate Prime S的相关文章

ZOJ 3772 Calculate the Function 线段树+矩阵

Calculate the FunctionTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-09) Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query

[矩阵+线段树] zoj 3772 Calculate the Function

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters

zoj 3772 Calculate the Function

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为F(X)*A(X+2)+F(X+1)=F(X+2),然后在构造矩阵 {1, A[x]}  {F(x+1)}  {F(X+2)} {1,    0 }*{F(X)    }={F(X+1)} 然后用线段数维护乘号左边的乘积: 1 #include <cstdio> 2 #include <cs

ZOJ 3483 Gaussian Prime(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4280 In number theory, a Gaussian integer is a complex number whose real and imaginary part are both integers. The Gaussian integers, with ordinary addition and multiplication of complex

zoj3707(Calculate Prime S)解题报告

1.计算(a/b)%c,其中b能整除a 设a=b*r=(bc)*s+b*t 则(b*t)为a除以bc的余数 r=c*s+t 而 (a/b)%c=r%c=t (a%bc)/b=(b*t)/b=t 所以对于b与c互素和不互素都有(a/b)%c=(a%bc)/b成立. 当bc不大时,先取模bc,再除b 如果b与c互素,则(a/b)%c=a*b^(phi(c)-1)%c 待证 2.与集合子集 斐波那契数列的第n+2项同时也代表了集合{1,2,...,n}中所有不包含相邻正整数的子集个数. 证明:归纳法证

线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 Mean: 略 analyse: 简单的线段树维护矩阵. 矩阵乘法的结合律(a * b * c == a * (b * c)),注意矩阵乘法不满足分配率(a *b != b * a). 令 M[x] = [1 A[x]]              [1     0 ] ,那么有 [ F

ZOJ 3772 Calculate the Function(矩阵线段树)

Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query: The query has two parameters Li and Ri. The query will define a function Fi(x) on the domain [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li + 1) = A(Li + 1) for all x >= Li +

ZOJ 3179 Calculate With Abacus(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3220 Abacus is a manual computing device consisting of a frame holding parallel rods strung with movable beads. How to calculate with Abacus: Each rod is divided into two parts by the se

zoj 3772 Calculate the Function(线段树+矩阵乘法)

Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters Li and Ri. The query will define a function Fi(x) on the domain [Li,