【算法练习】二分求幂/《王道》 3道题

目录

二分求幂

人见人爱A^B

A sequence of numbers

 Tr A


二分求幂

                                                                                                             -----------------------------------------------来自王道机试指南 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2035

人见人爱A^B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 62485    Accepted Submission(s): 41441


 

Problem Description

求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”

Input

输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。

Output

对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。

 

Sample Input

2 3 12 6 6789 10000 0 0

 

Sample Output

8 984 1

 理解题目:就是二分求幂 ,最后三位数

//人见人爱A^B
#include <iostream>
using namespace std;

int a,b;
int main(){
    while(cin>>a>>b){
        if(a==0 && b==0) break;
        int ans=1;

        while(b!=0){
            if(b%2==1){
                ans=ans*a;
                ans=ans%1000;  //最后三位数
            }
            b=b/2;
            a=a*a;
            a=a%1000;
        }
        cout<<ans<<endl;
    }
    return 0;
}

A sequence of numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8365    Accepted Submission(s): 2482

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817

Problem Description

Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help.

 

Input

The first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain four integers. The first three indicating the first three numbers of the sequence, and the last one is K, indicating that we want to know the K-th numbers of the sequence.

You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63). All the numbers of the sequences are integers. And the sequences are non-decreasing.

 

Output

Output one line for each test case, that is, the K-th number module (%) 200907.

 

Sample Input

2 1 2 3 5 1 2 4 5

 

Sample Output

5 16

题目解释:等差和等比数列判断,然后用公式计算 数字大要求模

错误点在于计算等差数列的时候几次MOD,我最后少MOD一次于是一直WA

可以写成#define 这样更好看出来有没有少%

if(a+c==b+b){  //等差数列
    ans=(a%200907+((k-1)%200907)*((b-a)%200907)%200907)%200907;  //最后没有%
}

等比数列快速幂求解,可以一开始就等于a   然后再*q^(n-1)

AC代码:

//等差数列或等比数列判断
#include <iostream>
using namespace std;

int N;
long long a,b,c,ans,k;  //把k改成long long

int main(){

    while(  cin>>N){
        while(N--){
            cin>>a>>b>>c>>k;
            //判断是等差数列 还是 等比数列
            if(a+c==b+b){  //等差数列
                ans=(a%200907+((k-1)%200907)*((b-a)%200907)%200907)%200907;  //最后没有%
            }
            else if(b*b==a*c){ //等比数列
                //二分求幂
                //q的k-1次方
                long long q=b/a;
                k--;
                ans=a;
                while(k!=0){
                    //转二进制
                    if(k%2==1){
                        ans*=q;
                        ans=ans%200907;
                    }

                    k=k/2;
                    q=q*q;
                    q=q%200907;
                }
                //ans=(ans*a)%200907;
            }

            cout<<ans<<endl;

        }
    }

    return 0;
}

 


 Tr A

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575

Tr A

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9350    Accepted Submission(s): 6793


 

Problem Description

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。

Input

数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。

Output

对应每组数据,输出Tr(A^k)%9973。

 

Sample Input

2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9

 

Sample Output

2 2686

 

题目理解:

矩阵快速幂

写一个矩阵乘法 然后用快速幂计算矩阵的幂

AC代码:

//
//A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
#include <iostream>
using namespace std;
#define MOD 9973
int T,n,k;
struct Matrix
{
    int arr[12][12];
}init,unit;

Matrix Mul(Matrix A,Matrix B){
    Matrix r;
    //左行×右列
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            r.arr[i][j]=0;
            for(int k=1;k<=n;k++){   //%MOD
                r.arr[i][j]=(r.arr[i][j]+(A.arr[i][k]*B.arr[k][j])%MOD)%MOD;
            }
        }
    }
    return r;
}

//A的次方
Matrix Pow(Matrix A,Matrix B,int x){
    while(x!=0){
        if(x%2==1){
            A=Mul(A,B);
        }
        B=Mul(B,B);
        x=x/2;
    }
    return A;
}
int main(){
    cin>>T;
    while(T--){
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>init.arr[i][j];
                unit.arr[i][j]=init.arr[i][j];
            }
        }

        Matrix res=Pow(init,unit,k-1);
        int ans=0;
        for(int i=1;i<=n;i++){
            ans=(ans+res.arr[i][i])%MOD;
        }
        cout<<ans<<endl;

    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值