Sumdiv

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Appoint description: 
 System Crawler  (2015-05-27)

Description

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901).

Input

The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.

Output

The only line of the output will contain S modulo 9901.

Sample Input

2 3

Sample Output

15

Hint

2^3 = 8. 
The natural divisors of 8 are: 1,2,4,8. Their sum is 15. 
15 modulo 9901 is 15 (that should be output). 
 
 

题意:求(A^B)的约数和对9901取余的结果。

思路:转载--->優YoU

解题思路:

要求有较强 数学思维 的题

应用定理主要有三个:

要求有较强 数学思维 的题

应用定理主要有三个:

(1)   整数的唯一分解定理:

任意正整数都有且只有一种方式写出其素因子的乘积表达式。

A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数

      因数个数:(k1+1)*(k2+1)*...*(kn+1)

(2)   约数和公式:

对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)

有A的所有因子之和为

S = (1+p1+p1^2+p1^3+...p1^k1) * (1+p2+p2^2+p2^3+….p2^k2) * (1+p3+ p3^3+…+ p3^k3) * .... * (1+pn+pn^2+pn^3+...pn^kn)

(3)   同余模公式:

(a+b)%m=(a%m+b%m)%m

(a*b)%m=(a%m*b%m)%m

有了上面的数学基础,那么本题解法就很简单了:

1: 对A进行素因子分解

分解A的方法:

A首先对第一个素数2不断取模,A%2==0时 ,记录2出现的次数+1,A/=2;

当A%2!=0时,则A对下一个连续素数3不断取模...

以此类推,直到A==1为止。

注意特殊判定,当A本身就是素数时,无法分解,它自己就是其本身的素数分解式。

最后得到A = p1^k1 * p2^k2 * p3^k3 *...* pn^kn.
      故 A^B = p1^(k1*B) * p2^(k2*B) *...* pn^(kn*B);

2:A^B的所有约数之和为:

sum = [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...* [1+pn+pn^2+...+pn^(an*B)].

3: 用递归二分求等比数列1+pi+pi^2+pi^3+...+pi^n:

(1)若n为奇数,一共有偶数项,则:
      1 + p + p^2 + p^3 +...+ p^n

= (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2) * (1+p^(n/2+1))
      = (1 + p + p^2 +...+ p^(n/2)) * (1 + p^(n/2+1))

上式红色加粗的前半部分恰好就是原式的一半,那么只需要不断递归二分求和就可以了,后半部分为幂次式,将在下面第4点讲述计算方法。

(2)若n为偶数,一共有奇数项,则:
      1 + p + p^2 + p^3 +...+ p^n

= (1+p^(n/2+1)) + p * (1+p^(n/2+1)) +...+ p^(n/2-1) * (1+p^(n/2+1)) + p^(n/2)
      = (1 + p + p^2 +...+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);

上式红色加粗的前半部分恰好就是原式的一半,依然递归求解

4:反复平方法计算幂次式p^n

这是本题关键所在,求n次幂方法的好坏,决定了本题是否TLE。

以p=2,n=8为例

常规是通过连乘法求幂,即2^8=2*2*2*2*2*2*2*2

这样做的要做8次乘法

而反复平方法则不同,

定义幂sq=1,再检查n是否大于0,

While,循环过程若发现n为奇数,则把此时的p值乘到sq

{

n=8>0 ,把p自乘一次, p=p*p=4     ,n取半 n=4

n=4>0 ,再把p自乘一次, p=p*p=16   ,n取半 n=2

n=2>0 ,再把p自乘一次, p=p*p=256  ,n取半 n=1,sq=sq*p

n=1>0 ,再把p自乘一次, p=p*p=256^2  ,n取半 n=0,弹出循环

}

则sq=256就是所求,显然反复平方法只做了3次乘法

 超棒的一道数论题

原文链接:http://blog.csdn.net/u013486414/article/details/46237349

    1. #include <stdio.h>
    2. #include <math.h>
    3. #include <string.h>
    4. #include <stdlib.h>
    5. #include <iostream>
    6. #include <sstream>
    7. #include <algorithm>
    8. #include <set>
    9. #include <queue>
    10. #include <stack>
    11. #include <map>
    12. using namespace std;
    13. typedef long long LL;
    14. const int inf=0x3f3f3f3f;
    15. const double eps=1e-10;
    16. const double pi= acos(-1.0);
    17. const int MAXN=1e5+10;
    18. const int mod=9901;
    19. LL Mul(LL a,LL b) {//快速乘法
    20. LL res=0;
    21. while(b>0) {
    22. if(b&1) res=(res+a)%mod;
    23. b>>=1;
    24. a=(a+a)%mod;
    25. }
    26. return res;
    27. }
    28. LL modxp(LL a,LL b) {//快速幂取余
    29. LL res=1;
    30. while(b>0) {
    31. if(b&1) res=Mul(res,a);
    32. b>>=1;
    33. a=Mul(a,a);
    34. }
    35. return res;
    36. }
    37. LL Sum(LL p,LL n) {//递归二分求 (1 + p + p^2 + p^3 +...+ p^n)%mod
    38. if(n==0)
    39. return 1;
    40. if(n&1)
    41. return ((1+modxp(p,n/2+1))%mod*Sum(p,n/2)%mod)%mod;
    42. else
    43. return ((1+modxp(p,n/2+1))%mod*Sum(p,(n-1)/2)%mod+modxp(p,n/2)%mod)%mod;
    44. }
    45. int main() {
    46. int A,B,i;
    47. int p[MAXN];//A的分解式p[i]^k[i];
    48. int k[MAXN];
    49. while(~scanf("%d %d",&A,&B)) {
    50. int cnt=0;
    51. for(i=2; i*i<=A; i++) {//分解整数A (A为非质数)
    52. if(A%i==0) {
    53. p[cnt]=i;
    54. k[cnt]=0;
    55. while(A%i==0) {
    56. A/=i;
    57. k[cnt]++;
    58. }
    59. cnt++;
    60. }
    61. }
    62. if(A!=1) {//特殊判定:分解整数A (A为质数)
    63. p[cnt]=A;
    64. k[cnt]=1;
    65. cnt++;
    66. }
    67. int res=1;
    68. for(i=0; i<cnt; i++)
    69. res=(res%mod*Sum(p[i],B*k[i])%mod);
    70. printf("%d\n",res);
    71. }
    72. return 0;
    73. }

POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)的更多相关文章

  1. POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]

    传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有 ...

  2. 快速幂取模(POJ 1995)

    http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c ...

  3. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  4. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  5. Powmod快速幂取模

    快速幂取模算法详解 1.大数模幂运算的缺陷: 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算 ...

  6. hdu2065"红色病毒"问题(指数母函数+快速幂取模)

    "红色病毒"问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU-2817,同余定理+快速幂取模,水过~

    A sequence of numbers                                                             Time Limit: 2000/1 ...

  8. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  9. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

随机推荐

  1. struts2中拦截器与过滤器之间的区别

    首先是一张经典的struts2原理图 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) ...

  2. 九度OJ 1053:互换最大最小数 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6613 解决:2676 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后输出. 输入: 测试数据有多组 ...

  3. Cocos2d-x中定时器的使用

    CCTimer:轻量级的计时器 CCTimer (void) ccTime  getInterval (void) void  setInterval (ccTime fInterval) bool  ...

  4. BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1680 题意: 在接下来的n周内,第i周生产一吨酸奶的成本为c[i],订单为y[i]吨酸奶. ...

  5. hdu-5784 How Many Triangles(计算几何+极角排序)

    题目链接: How Many Triangles Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  6. codeforces 566D D. Restructuring Company(并查集)

    题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. linux命令学习笔记(52):ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有 一个类似的工具,也就是ifconfig (interfaces config).通 ...

  8. 集训Day11

    别人的题公开原题面不好 写题解吧 T1 很明显答案满足二分,二分之后算出每个人的位置,做一个LIS即可 T2 阅读体验极差 T3 根本不会 交都没交 期望得分200

  9. float浮动改变display类型

    position:absolute和float都会隐式的改变display类型. 也就是说,不论之前是什么类型的元素(display:none除外),只要设置了position:absolute或fl ...

  10. C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument

    &apos;System.Collections.Generic.IEnumerable<string>&apos; does not contain a definiti ...