hdu-5525 Product(费马小定理)
题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003

前面用奇偶性约掉2,后面处理前缀积和后缀积。
WA了很久的地方:在约掉2之前不能模(mod-1)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 100001;
LL Mod = 1000000007;
LL mod = Mod - 1;
char vis[N];
int prime[N];
vector<int> G[N];
int tot = 0;
void init_prime()
{
memset(vis, 0, sizeof(vis));
for(int i=2; i<N; i++)
{
if(!vis[i])
{
prime[++tot] = i;
for(int j=i; j<N; j+=i)
{
vis[j] = 1;
G[j].push_back(i);
}
}
}
}
LL quick(LL a, LL b)
{
LL c = 1;
while(b)
{
if(b&1)
c = c * a % Mod;
b >>= 1;
a = a * a % Mod;
}
return c;
}
int a[N];
LL num[N]; //素数i的个数
LL pre[N], suf[N];
int main()
{
init_prime();
int i, j, k, m, n;
while(scanf("%d", &n) == 1)
{
for(i=1; i<=n; i++)
scanf("%d", a+i);
memset(num, 0, sizeof(num));
for(i=1; i<=n; i++)
{
for(j=0; j<G[i].size(); j++)
{
int tp = 0, x = i;
while(x%G[i][j] == 0)
{
tp ++;
x /= G[i][j];
}
num[G[i][j]] += a[i] * tp;
}
}
///处理(p1+1)*(p2+1)*...*(px+1)的前缀积和后缀积
pre[0] = 1;
for(int i=1; i<=tot; i++)
{
pre[i] = pre[i-1];
if(num[prime[i]])
pre[i] = pre[i] * (num[prime[i]] % mod +1) % mod;
}
suf[tot+1] = 1;
for(int i=tot; i>=1; i--)
{
suf[i] = suf[i+1];
if(num[prime[i]])
suf[i] = suf[i] * (num[prime[i]] % mod +1) % mod;
}
/*---------------------------------------------*/
LL ans = 1;
for(int i=1; i<=tot; i++)
{
LL tmp, p = num[prime[i]];
if(p & 1)
tmp = p % mod * (((p+1)>>1) % mod) % mod;
else
tmp = (p>>1) %mod * ((p+1) % mod) % mod;
ans = ans * quick(prime[i], tmp * pre[i-1] % mod * suf[i+1] % mod) % Mod;
}
printf("%I64d\n", ans);
}
return 0;
}
hdu-5525 Product(费马小定理)的更多相关文章
- hdu 4704 Sum 费马小定理
题目链接 求2^n%mod的值, n<=10^100000. 费马小定理 如果a, p 互质, 那么a^(p-1) = 1(mod p) 然后可以推出来a^k % p = a^(k%(p-1) ...
- HDU - 6440(费马小定理)
链接:HDU - 6440 题意:重新定义加法和乘法,使得 (m+n)^p = m^p + n^p 成立,p是素数.,且satisfied that there exists an integer q ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Outp ...
- hdu 4704(费马小定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704 思路:一道整数划分题目,不难推出公式:2^(n-1),根据费马小定理:(2,MOD)互质,则2^ ...
- HDU 5667 Sequence【矩阵快速幂+费马小定理】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...
- hdu 4704 Sum(组合,费马小定理,快速幂)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4704: 这个题很刁是不是,一点都不6,为什么数据范围要开这么大,把我吓哭了,我kao......说笑的, ...
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- HDU 5667 Sequence 矩阵快速幂+费马小定理
题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...
随机推荐
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 使用css让div半透明
设置div的背景为半透明,下面有个不错示例,使用div+css来实现下,不会的朋友可以参考下 div{filter:alpha(Opacity=80);-moz-opacity:0.5;opacity ...
- 打开QQ会话
Android:String url="mqqwpa://im/chat?chat_type=wpa&uin=123456";startActivity(new Inten ...
- iOS传值之block传值(一)
ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...
- tslib1.4编译
准备工作:ubuntu14.04-64位+arm-linux-gcc(2014)+tslib.14 1.arm-linux-gcc下载地址:http://www.veryarm.com/arm-non ...
- 分布式入门之5:paxos
paxos是去中心化协议,较难理解. proposer, accepter是其中的主要角色.前者发起投票,后者批准投票. 核心思想是,一旦超过半数的accepter同意某个投票,整个流程结束,批准 ...
- Apache_proxy负载均衡和Session复制
今天上网查了查资料,之前使用apache的jk模块做负载均衡.后来觉得jk的负载配置有点死板,只能按照负载权重值来进行请求的分发,没有做到比较智能的负载平衡,并且使用mod_jk访问页面发现确实比较慢 ...
- java获取两个时间的相隔时间,包括年、月、日、时、分、秒
public static final int YEAR_RETURN = 0; public static final int MONTH_RETURN = 1 ...
- mac攻略(七) -- 环境变量PATH分析
一.首先需要了解 1>mac 一般使用bash作为默认shell 2>Mac系统的环境变量,加载顺序为: 1.系统级别的 /etc/profile /etc/bashrc /etc/p ...
- 大数据 > 数据平台方案评估
分类 当前措施 说明 百度竞价如何进行数据分析(SEM工程师)数据来源: 1. 百度后台推广数据:api 总展现 总点击 点击率 总消费 点击均价 BDP功能点 1. 串联百度->网站商务通-& ...