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.

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

0

Sample Output

6

10

设F(x)=2004x的因子和F(x)=2004^x的因子和F(x)=2004x的因子和 因为这是个积性函数,则有f(N)=∏i=1nf(qiqi)其中N可以表示为∏i=1nqiqif(N)=\prod_{i=1}^nf(q_i ^{q_i}) 其中 N可以表示为\prod_{i=1}^nq_i ^{q_i}f(N)=∏i=1n​f(qiqi​​)其中N可以表示为∏i=1n​qiqi​​

f(2004n)=f(2(2∗n))∗f(3n)∗f(167n)=(2(2∗n+1)−1)∗(3(n+1)−1)/2∗(167(n+1)−1)/166f(2004 ^ n)= f(2 ^{(2 * n)})* f(3 ^ n)* f(167 ^ n)
=(2 ^{(2 * n + 1)}-1)*(3 ^{(n + 1)}-1)/ 2 *(167 ^{(n + 1)}-1)/ 166f(2004n)=f(2(2∗n))∗f(3n)∗f(167n)=(2(2∗n+1)−1)∗(3(n+1)−1)/2∗(167(n+1)−1)/166

用到乘法逆元:(同余性质)

a ^ k / d = a ^ k *(d-1)d-1即为d的逆元。3的逆元为15 167的逆元为18

JAVA C++ 没区别,最近在JAVA要考试了额,熟悉一下。

import java.util.Scanner;

public class Main {
public static long Q_pow( long a, long p, long mod)
{
long ans = 1%mod;
while(p>0) {
if(p%2==1) ans = ans*a%mod; //防止在对P取模前溢出
a = a*a%mod;
p >>=1; //比除法快多了
}
return ans;
}
public static void main(String[] args) {
int n;
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
n=in.nextInt();
if(n==0) break;
long ans=((Q_pow(167,n+1,29)-1)*18)%29;
ans=(ans*((Q_pow(3,n+1,29)-1)*15)%29)%29;
ans=(ans*(Q_pow(2,2*n+1,29)-1))%29;
System.out.println(ans);
}
in.close();
} }

数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)的更多相关文章

  1. 数学--数论--HDU1825(积性函数性质+和函数公式+快速模幂+非互质求逆元)

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  2. 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

    Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...

  3. HDU 1452 Happy 2004 (逆元+快速幂+积性函数)

    G - Happy 2004 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  4. HDU 1452 Happy 2004(因子和的积性函数)

    题目链接 题意 : 给你一个X,让你求出2004的X次方的所有因子之和,然后对29取余. 思路 : 原来这就是积性函数,点这里这里这里,这里讲得很详细. 在非数论的领域,积性函数指所有对于任何a,b都 ...

  5. HDU 1452 Happy 2004(因数和+费马小定理+积性函数)

    Happy 2004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  6. hdu 1452 Happy 2004

    因子和: 的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12; 的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42; 的因子是1,2 ...

  7. Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

    Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...

  8. 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)

    2749: [HAOI2012]外星人 Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Ou ...

  9. 数学--数论--HDU 12151七夕节 Plus (因子和线性筛)

    Problem Description 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" ...

随机推荐

  1. JavaScript form表单提交与验证

    原网址:https://blog.csdn.net/vipwxs/article/details/79119701 一.form对象的属性: name:获取表单的名称,该name一般给JS使用 met ...

  2. gdb调试工具常用命令 && kdb

    编译程序时需要加上-g,之后才能用gdb进行调试:gcc -g main.c -o main gdb中命令: 回车键:重复上一命令 (gdb)help:查看命令帮助,具体命令查询在gdb中输入help ...

  3. "一号标题"组件:<h1> —— 快应用组件库H-UI

     <import name="h1" src="../Common/ui/h-ui/text/c_h1"></import> < ...

  4. tcp协议:三次握手四次挥手详解-转

    https://www.cnblogs.com/welan/p/9925119.html

  5. R语言kohonen包主要函数介绍

    最近准备写一篇关于自组织映射 (Self-organizing map)的文章.SOM的代码很多,研究了一圈之后目前使用最顺手的是R语言的kohonen包. 这个kohonen包功能很丰富,但是接口不 ...

  6. B - Fadi and LCM CodeForces - 1285C 质因子

    题目大意很简单,给你一个整数X,让你求a和b,使得max(a,b)尽可能的小,然后打印a,b 题解:想到了质因子分解,也考虑到了暴力,但是觉得暴力的话会TLE,所以打算用贪心做,然后就一直Wa.... ...

  7. 不同目录有同名proto文件情况下,protoc生成.cc/.h

    首先先参考一下别人的博客,看完了,看懂了,再回过头来看我下面说的情况. 链接 https://blog.csdn.net/CAir2/article/details/78201572 但是这个也就是基 ...

  8. 详解 HashMap

    本篇博文的知识点,在我们的日常生活中,应用十分广阔.比如:每个学生,都有自己的对应的学号.每一个公民,都有自己的身份证号- - 相信看到这里,有的同学基本上已经猜到了这个类的主要用途.那么,话不多说, ...

  9. SpringBoot+Netty+WebSocket实现实时通信

    这篇随笔暂时不讲原理,首先搭建起一个简单的可以实现通信的Demo.之后的一系列随笔会进行一些原理上的分享. 不过在这之前大家最好了解一下Netty的线程模型和NIO编程模型,会对它的整体逻辑有所了解. ...

  10. numpy+sklearn 手动实现逻辑回归【Python】

    逻辑回归损失函数: from sklearn.datasets import load_iris,make_classification from sklearn.model_selection im ...