日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

HOJ 1991 Happy 2005 HOJ 2635 Weights 快速幂

發布時間:2025/7/14 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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_JUDGEfreopen("sum.in","r",stdin); #endifll 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)
代碼略。。。

轉載于:https://www.cnblogs.com/yejinru/archive/2013/01/18/2866065.html

總結

以上是生活随笔為你收集整理的HOJ 1991 Happy 2005 HOJ 2635 Weights 快速幂的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。