HOJ 1991 Happy 2005 HOJ 2635 Weights 快速幂
http://acm.hit.edu.cn/hoj/problem/view?id=1991
HOJ 1991 Happy 2005
| ? | 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 0Sample 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的時候,我們可以直接轉化為對矩陣進行
快速冪取模。
我下面的代碼為構造矩陣求解。。。
?
Weights
| ??(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 1000000Sample 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 快速幂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CVTRES : fatal error
- 下一篇: C# webbrowser控件点击页面按