• H


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

    Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.

    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. 

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

    Sample Input

    1
    10000
    0

    Sample Output

    6
    10

    即求2000^X%29,2004分解为2*2*3*167=2^2*3*167,即求2^2X%29*3^X%29*167^X%29.

    3^X等比求和,减一,(因为X!=0)3^X=(3^X-1)/(3-1)=(3^X-1)/2%29,取模没有除,所以用逆元:

    用费马小定理求逆元:

    用快速幂。求得a^p-2.即代码如下

    #include <iostream>
    #include <cstdio>
    typedef long long ll;
    using namespace std;
    ll fun(ll n,ll a,ll p)
    {
        ll ans=1;
        while(a)
        {
            if(a%2!=0) ans=ans*n%p;
            n=n*n%p;
            a/=2;
        }
        return ans%p;
    }
    int main()
    {
        int n,x;
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            ll a=fun(2,2*n+1,29)-1;
            ll b=(fun(3, n + 1, 29) - 1) * fun(2, 27, 29);//2的逆元
            ll c=(fun(167, n + 1, 29) - 1) * fun(166, 27, 29);//166的逆元
            printf("%lld
    ",(a*b*c)%29);
        }
        return 0;
    }
    


     

  • 相关阅读:
    Java反编译插件Jad及eclipse编译插件JadClipse综合使用
    Java IO示例总结
    java Statement与preparedStatement的区别
    Android TabHost 文字及图片的设置
    Android EditText属性
    ubuntu 软件安装及卸载
    Android 设置控件不可见且不占用空间
    Ubuntu 11 安装后要做的20件事情
    Ubuntu Samba安装与创建目录
    Android Service
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319563.html
Copyright © 2020-2023  润新知