zoukankan      html  css  js  c++  java
  • POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

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

    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

    [html] view plain copy
     
      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.   
      46. int main() {  
      47.     int A,B,i;  
      48.     int p[MAXN];//A的分解式p[i]^k[i];  
      49.     int k[MAXN];  
      50.     while(~scanf("%d %d",&A,&B)) {  
      51.         int cnt=0;  
      52.         for(i=2; i*i<=A; i++) {//分解整数A (A为非质数)  
      53.             if(A%i==0) {  
      54.                 p[cnt]=i;  
      55.                 k[cnt]=0;  
      56.                 while(A%i==0) {  
      57.                     A/=i;  
      58.                     k[cnt]++;  
      59.                 }  
      60.                 cnt++;  
      61.             }  
      62.         }  
      63.         if(A!=1) {//特殊判定:分解整数A (A为质数)  
      64.             p[cnt]=A;  
      65.             k[cnt]=1;  
      66.             cnt++;  
      67.         }  
      68.         int res=1;  
      69.         for(i=0; i<cnt; i++)  
      70.             res=(res%mod*Sum(p[i],B*k[i])%mod);  
      71.         printf("%d ",res);  
      72.     }  
      73.     return 0;  
      74. }
  • 相关阅读:
    置换群
    背包问题
    并查集
    链式前向星
    一个简单的金额平均分配函数(C#版)
    EasyUI ComboGrid的绑定,上下键和回车事件,输入条件查询
    Oracle表解锁语句
    如何将两个json合并成一个
    textbox只能输入数字或中文的常用正则表达式和验证方法
    C#注册表的读,写,删除,查找
  • 原文地址:https://www.cnblogs.com/yzm10/p/8360262.html
Copyright © 2011-2022 走看看