zoukankan      html  css  js  c++  java
  • SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME

    Problem code: BFALG

     

    Please click here to download a PDF version of the contest problems. The problem is problem B in the PDF. But the data limits is slightly modified: 1≤P≤1000000 in the original description, but in this EXTREME version, 1≤P≤1000000000.

    =========(EDIT, Francky)===============

    Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:

    Function Find(integer n,function func)
    	If n=1
    		For i = 1 to a do func()
    	Elseif n=2
    		For i = 1 to b do func()
    	Else Find(n-1,Find(n-2,func))
    Function Main
    	Find(n,funny)
    

    Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
    Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.

    Input

    There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.
    For each test cases, there are four integers a, b, P and n in a single line. You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.

    Output

    For each test case, output the answer with case number in a single line.

    Example

    Input:
    3
    3 4 10 3
    4 5 13 5
    3 2 19 100
    
    Output:
    Case #1: 2
    Case #2: 11
    Case #3: 12

    公式稍微列一下就可以发现是
    次数 a,b
    1: 1,0
    2: 0,1
    3: 1,1
    4: 1,2
    5: 2,3....
    可以看出结果与斐波那契数列有关,
    是a^f(n-3)*b^f(n-2),
    但是斐波那契数列是用指数形式增长的,很快就会超出64位,而且直接运算肯定会超时,
    那么
    1.为了解决时间问题,使用矩阵快速幂,
    {f(n-1),fn, {0,1, {fn,fn+f(n-1),
    0, 0, }* 1,1, }= 0, 0}
    2.为了解决斐波那契数字过大问题,有公式
    a^c%P=a^(c%phi(P)+phi(P))%P
    其中phi是欧拉函数

    耽误时间主要原因
    1 一开始想要把1-1e6所有欧拉函数值都求出来
    2
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    int a,b,P,n;
    ll s[2][2],t[2][2];
    ll phi;
    void calphisub(){
            int tP=P;
            phi =P;
            if((tP&1)==0){
                    phi>>=1;
                     while((tP&1)==0){
                            tP>>=1;
                     }
            }
            for(int i=3;i*i<=tP;i+=2)
           {
               if(tP%i==0)
              {
                  phi=phi/i*(i-1);
                  while(tP%i==0)
                  {
                      tP/=i;
                  }
             }
          }
          if(tP>1)phi=phi/tP*(tP-1);
    }
    void multi(ll a[2][2],ll b[2][2] ,ll c[2][2] ){
            ll tmp[2][2];
            for(int i=0;i<2;i++){
                    for(int j=0;j<2;j++){
                            tmp[i][j]=0;
                            for(int k=0;k<2;k++){
                                    tmp[i][j]+=a[i][k]*b[k][j];
                                    if(tmp[i][j]>phi){
                                            tmp[i][j]=tmp[i][j]%phi+phi;
                                    }
                            }
                    }
            }
            for(int i=0;i<2;i++){
                    for(int j=0;j<2;j++){
                            c[i][j]=tmp[i][j];
                    }
            }
    }
    void init(){
            s[0][0]=1,s[0][1]=1,s[1][0]=0,s[1][1]=0;
            t[0][0]=0,t[0][1]=1,t[1][0]=1,t[1][1]=1;
    }
    void qpow(int n){
            while(n>0){
                    if(n%2==1){
                            multi(s,t,s);
                    }
                    multi(t,t,t);
                    n/=2;
            }
    }
    ll qpow2(int n,ll sub){
            ll ans=1;
            while(n>0){
                    if((n&1)!=0){
                            ans=ans*sub%P;
                    }
                    sub=sub*sub%P;
                    n/=2;
            }
            return ans;
    }
    void getfab(int n,ll& fn,ll& fminus){
            if(n==1){
                    fn=0;fminus=1;
            }
            else if(n==2){
                    fn=1;fminus=0;
            }
            else {
                    init();
                    qpow(n-3);
                    fminus=s[0][0];
                    fn=s[0][1];
            }
    }
    int main(){
            int T;
            scanf("%d",&T);
            for(int i=0;i<T;i++){
                    scanf("%d%d%d%d",&a,&b,&P,&n);
                    if(P==1){ printf("Case #%d: 0
    ",i+1);continue;}
                    ll ta,tb;
                    calphisub();
                    getfab(n,tb,ta);
                    ll pa=qpow2(ta,a)%P;
                    ll pb=qpow2(tb,b)%P;
                    ll ans=pa*pb%P;
                    printf("Case #%d: %I64d
    ",i+1,ans);
            }
    }
    

      

      

  • 相关阅读:
    linux C gcc -lm
    ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了
    ArrayList调用remove方法需要注意的地方
    关于Java中File的renameTo函数
    Java管道流
    NPOI Excel 单元格背景颜色对照表
    Java 简单图片截取
    maven pom.xml 配置 cxf-codegen-plugin 生成web服务客户类型
    ZeroClipboard 简单应用
    PromiseJs
  • 原文地址:https://www.cnblogs.com/xuesu/p/4261572.html
Copyright © 2011-2022 走看看