Happy 2004

问题描述 :

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.

样例输入:

1
10000
0

样例输出:

6
10

设S(x)表示x的因子和。则题目求为:S(2004^X)mod 29
因子和S是积性函数,即满足性质1。

性质1 :如果 gcd(a,b)=1  则 S(a*b)= S(a)*S(b)
2004^X=4^X * 3^X *167^X
S(2004^X)=S(2^(2X)) * S(3^X) * S(167^X)

性质2 :如果 p 是素数 则 S(p^X)=1+p+p^2+…+p^X = (p^(X+1)-1)/(p-1)
因此:S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (167^(X+1)-1)/166
167%29 == 22
S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (22^(X+1)-1)/21

性质3 :(a*b)/c %M= a%M * b%M * inv(c)
其中inv(c)即满足 (c*inv(c))%M=1的最小整数,这里M=29
则inv(1)=1,inv(2)=15,inv(22)=15

有上得:
S(2004^X)=(2^(2X+1)-1) * (3^(X+1)-1)/2 * (22^(X+1)-1)/21
=(2^(2X+1)-1) * (3^(X+1)-1)*15 * (22^(X+1)-1)*18

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const double eps=1e-;
int p=;
LL pow_mod(LL x,LL n)
{
LL res=;
while(n>)
{
if(n&) res=res*x%p;
x=x*x%p;
n>>=;
}
return res;
}
int main()
{
LL x,i;
while(cin>>x&&x)
{
int a=pow_mod(,*x+);
int b=pow_mod(,x+);
int c=pow_mod(,x+);
int s=((a-)*(b-)*(c-)**)%;
cout<<s<<endl;
} }

Happy 2004(快速幂+乘法逆元)的更多相关文章

  1. hdu-4990 Reading comprehension(快速幂+乘法逆元)

    题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  2. 51Nod 1013 3的幂的和 快速幂 | 乘法逆元 | 递归求和公式

    1.乘法逆元 直接使用等比数列求和公式,注意使用乘法逆元 ---严谨,失细节毁所有 #include "bits/stdc++.h" using namespace std; #d ...

  3. hdu-5690 All X(快速幂+乘法逆元)

    题目链接: All X Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Pro ...

  4. HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU4869:Turn the pokers(快速幂求逆元+组合数)

    题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...

  6. 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  7. NOIP2011多项式系数[快速幂|组合数|逆元]

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  8. [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理

    方便复制 快速乘/幂 时间复杂度 \(O(\log n)\). ll nmod; //快速乘 ll qmul(ll a,ll b){ ll l=a*(b>>hb)%nmod*(1ll< ...

  9. XTU 1260 - Determinant - [2017湘潭邀请赛A题(江苏省赛)][高斯消元法][快速幂和逆元]

    是2017江苏省赛的第一题,当时在场上没做出来(废话,那个时候又不懂高斯消元怎么写……而且数论也学得一塌糊涂,现在回来补了) 省赛结束之后,题解pdf就出来了,一看题解,嗯……加一行再求逆矩阵从而得到 ...

随机推荐

  1. Keil C51 详细设置

    一.target名更改 打开Keil后,左侧Project Workspace中的target可改,方法:右击Target——Manage Compnents——双击待修改项即可,若要添加,使用对话框 ...

  2. iostat 离线安装

    由于lucene需要一定的io读写顾安装iostat来对磁盘io进行监控 iostat 属于sysstat下的功能 git路径如下:https://github.com/sysstat/sysstat ...

  3. unix c 09

    IPC - 进程间通信   文件/信号/管道/共享内存/消息队列/信号量集/网络   XSI IPC (共享内存.消息队列和信号量集)   使用方式 非常的类似. 共享内存的使用步骤:     1 生 ...

  4. Palindrome Partitioning 解答

    Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...

  5. Android 打造自己的个性化应用(三):应用程序的插件化

    在android的项目开发中,都会遇到后期功能拓展增强与主程序代码变更的现实矛盾,也就是程序的灵活度. 由于linux平台的安全机制,再加上dalvik的特殊机制,各种权限壁垒,使得开发一个灵活多变的 ...

  6. 怎么实现类似星星闪烁的效果(box-shadow)

    有时候设计希望我们能够在页面实现类似星星闪烁的效果,如图: 我的解决办法是用box-shadow: html <div class="star04 active-blink" ...

  7. css-布局1-基本属性

    <!DOCTYPE html>CSS4-布局1-基本属性 属性:displayvisibilityfloatclear HTML元素类型:行内元素,块级元素 块级元素:最大的区别:换行行内 ...

  8. avalon2学习心得(1)

    github上,avalon2的项目描述是这样的:“avalon2是一款基于虚拟DOM与属性劫持的 迷你. 易用. 高性能 的 前端MVVM框架, 适用于各种场景, 兼容各种古老刁钻浏览器, 吸收最新 ...

  9. Ubantu 命令

    进入窗口删除文件(所有文件都可以删除) gksudo nautilus 输入法问题 ibus-daemon -drx

  10. MVC 错误处理1

    实例1. /// <summary> /// 错误处理 /// 404 处理 /// </summary> protected void Application_Error(o ...