zoukankan      html  css  js  c++  java
  • 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;
    }
    


     

  • 相关阅读:
    redis总结
    java程序启动脚本
    mysql生成百万测试数据脚本
    java分布式锁的实现及在springboot中的应用
    mysql使用总结
    一个java实现代码(服务)编排的思路(未完)
    sentinel自定义统一限流降级处理
    shell学习
    oracle查看被锁的事务进程sql
    Sql查询两个时间段有重叠的记录
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319563.html
Copyright © 2011-2022 走看看