HOJ 1991 Happy 2005 HOJ 2635 Weights 快速幂

http://acm.hit.edu.cn/hoj/problem/view?id=1991

HOJ 1991 Happy 2005

My Tags 矩阵快速幂   (Edit)
  Source : SCU Programming Contest 2005 Preliminary
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 344, Accepted : 223

Consider a positive integer X, and let S be the sum of all positive integer divisors of 2005X. Your job is to determine S modulo 29 (the rest of the division of 29).

Take X = 1 for example. The positive integer divisor of 20051 are 1 5 401 2005. Therefore S = 2412, and S modulo 29 is equal to 5.

Input

The input consists of several test cases. Each test case contains a line with the integer X(1 <= X <= 10000000).

A test case of X = 0 indicates the end of input, and should not be processed.

Output

For each test case, in a separate line, please output the result of S modulo 29.

Sample Input

1

10000

0

Sample Output

5

2

题目:
给出n,问2005^n的各个因子数之和对29取模

分析:
2005 = 5*401,我们可以对于401进行分类:
401^0 : 1 5 5^2 ... 5^n
401^1 : 401 401*5 401*5^2 ... 401*5^n
.
.
.
401^n : 401^n 401^n*5 401^n*5^2 ... 401^n*5^n
由此我们可以发现,问题可以转换为
(1+401+401^2+...401^n)*(1+5+5^2+...+5^n)%29

方法一:
二分再二分。首先,a^n用一次二分,求和的时候再用一次二分。
a^n二分的时候就是快速幂。
求和二分:
A+A^2+A...+A^(2k+1)= A+A^2+...+A^k+A^(k+1)+A^(k+1)*(A+A^2+...+A^k).
A+A^2+...+A^2k = A+A^2+...+A^k+A^k*(A+A^2+...+A^k).
方法二:
构造矩阵matrix如下:
A 1
0 1
我们发现matrix^(n+1)项的时候,第一行第二列就是问题所求
所以在求A+A^2+A^3+...+A^k % 29的时候,我们可以直接转化为对矩阵进行
快速幂取模。
我下面的代码为构造矩阵求解。。。

#include <cstdio>

#include <cstring>

#include <iostream>



using namespace std;



typedef long long ll;



#define debug puts("here");



const int X = 3;

const int MOD = 29;



struct Matrix{



    ll a[X][X];

    int n;

    int mod;



    Matrix(){}

    Matrix(int _n,int _mod):n(_n),mod(_mod){

        memset(a,0,sizeof(a));

    }



    void setE(){

        memset(a,0,sizeof(a));

        for(int i=1;i<=n;i++)

            a[i][i] = 1;

    }



    Matrix operator * (Matrix p){

        Matrix c(n,mod);

        for(int i=1;i<=n;i++)

            for(int j=1;j<=n;j++)

                for(int k=1;k<=n;k++){

                    c.a[i][j] += a[i][k]*p.a[k][j];

                    c.a[i][j] %= mod;

                }

        return c;

    }



    Matrix pw(int exp){

        Matrix cur = *this;

        Matrix c(n,MOD);

        c.setE();

        while(exp>0){

            if(exp&1)

                c = c*cur;

            cur = cur*cur;

            exp = exp>>1;

        }

        return c;

    }



    void di(){

        for(int i=1;i<=n;i++){

            for(int j=1;j<=n;j++)

                cout<<a[i][j]<<" ";

            cout<<endl;

        }

    }



    void init(int x){

        a[1][2] = a[2][2] = 1;

        a[2][1] = 0;

        a[1][1] = x;

    }

}matrix;



int main(){



#ifndef ONLINE_JUDGE

	freopen("sum.in","r",stdin);

#endif



    ll n;

    while(cin>>n,n){

        Matrix a = Matrix(2,MOD);

        a.init(5);

        a = a.pw(n+1);

        ll ans = a.a[1][2]%MOD;



        a.init(401);

        a = a.pw(n+1);

        ans = ans*a.a[1][2]%MOD;

        cout<<ans<<endl;

    }

	return 0;

}

  

 

Weights

My Tags   (Edit)
  Source : mostleg
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 276, Accepted : 103

Usually, given a balance and a group of weights (weight means poise here, and somewhere else in this problem), you will put some weights in the right plate of the balance, and add something in the left plate to make it balance.

This time you can use the balance as a different way, however, in which you can put the weights either in the left or right plate. As a result, the number of weights that can be denoted become far more than before.

Here is your task: Design the group of weights so that most consecutive integers from 1 (Unit: gram) can be denoted. Notice that every weight must be an integer (Unit: gram).

Input

The input consists of multiple test cases. In each test case, There is only an integer, n, (1<=n<=10^9), which is the number of weights.

Output

Normally, the result should be the maximum in the consecutive integer sequence that can be denoted, but it may be very large. So you need only output the result of the maximum value mod 9999997 instead. For every case, you should output only one integer.

Sample input

1

2

1000000

Sample output

1

4

4328126

题解:同上题。。。求的是1+3^1+...+3^(n-1)
代码略。。。

你可能感兴趣的:(APP)