[hdu5525 Product]暴力
题意:给定n和a[],令N = ∏(1≤i≤n)ia[i],求N的所有约数的积(取模1e9+7)
思路:
- 假定N因式分解后的结果是2p1*3p2*5p3*...,如何计算答案呢?
- 单独看2p1这一项,考虑它所有的贡献,它在N的约数里面总共会出现P=(p2+1)*(p3+1)*...次,由于是求乘积,而且2的指数可以取[0,p1],那么它总共产生的答案为(20)P*(21)P*(22)P*...=2(p1+1)*p1/2 * P=2p1*M/2,其中M=(p1+1)*(p2+1)*(p3+1)*...
- 因此答案就是2p1*M/2*3p2*M/2*5p3*M/2*...
- 所以首先把N的因式分解式求出来,然后按照上述公式求解即可
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
#include "local.h"
#endif const int mod = 1e9 + 7;
const int N = 1e5 + 7; vector<int> prime;
ll total[N];
bool used[N];
int a[N], ID[N]; void getPrime() {
for (ll i = 2; i < N; i ++) {
if (!used[i]) {
prime.pb(i);
for (ll j = i * i; j < N; j += i) used[j] = true;
}
}
for (int i = 0; i < prime.size(); i ++) {
ID[prime[i]] = i;
}
} pair<vector<int>, vector<int> > Y[N]; vector<int> fac;
vector<int> cnt; void div(int x) {
if (x > 2 && !used[x]) {
fac.pb(x);
cnt.pb(1);
return;
}
for (int i = 0; i < prime.size() && prime[i] <= x; i ++) {
int c = 0;
while (x % prime[i] == 0) {
c ++;
x /= prime[i];
}
if (c) {
fac.pb(prime[i]);
cnt.pb(c);
}
if (x < prime[i]) break;
}
} ll powermod(ll a, ll n, ll mod) {
ll ans = 1;
while (n) {
if (n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
getPrime();
for (int i = 1; i < N; i ++) {
fac.clear();
cnt.clear();
div(i);
Y[i] = mp(fac, cnt);
}
int n;
while (cin >> n) {
for (int i = 1; i <= n; i ++) {
scanf("%d", a + i);
}
mset(total, 0);
for (int i = 1; i <= n; i ++) {
for (int j = 0; j < Y[i].first.size(); j ++) {
int id = ID[Y[i].first[j]];
total[id] += Y[i].second[j] * a[i];
}
}
ll M = 1, ans = 1;
ll Mod = 2 * mod - 2;
for (int i = 0; i < prime.size(); i ++) {
M = M * ((total[i] + 1) % Mod) % Mod;
}
for (int i = 0; i < prime.size(); i ++) {
ans = ans * powermod(prime[i], M * (total[i] % Mod) % Mod / 2, mod) % mod;
}
cout << ans << endl;
}
return 0;
}
[hdu5525 Product]暴力的更多相关文章
- UVA.10305 Maximum Product (暴力)
UVA.10305 Maximum Product (暴力) 题意分析 直接枚举起点和重点,然后算出来存到数组里面,sort然后取最大值即可. 代码总览 #include <iostream&g ...
- hdu-5525 Product(费马小定理)
题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...
- Codeforces Round #670 (Div. 2) B. Maximum Product (暴力)
题意:有一长度为\(n\)的序列,求其中任意五个元素乘积的最大值. 题解:先排序,然后乘积能是正数就搞正数,模拟一下就好了. 代码: int t; ll n; ll a[N]; int main() ...
- PE刷题记
PE 中文翻译 最喜欢做这种很有意思的数学题了虽然数学很垃圾 但是这个网站的提交方式好鬼畜啊qwq 1.Multiples of 3 and 5 直接枚举 2.Even Fibonacci numbe ...
- 暴力求解——最大乘积 Maximum Product,UVa 11059
最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...
- UVa 11059 - Maximum Product 最大乘积【暴力】
题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...
- UVA 11059 Maximum Product【三层暴力枚举起终点】
[题意]:乘积最大的子序列.n∈[1,10],s∈[-10,10] [代码]: #include<bits/stdc++.h> using namespace std; int a[105 ...
- 利用Python自动生成暴力破解的字典
Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...
- [leetCode][001] Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
随机推荐
- D. Feeding Chicken(构造)
题目大意:将k个鸡放到一个n*m的矩阵中,要求每个鸡所占的rice的个数只差最小 题解:构造,设一共有cnt个rice,可以分cnt/k个,即每一只鸡要么占用cnt/k个rice,要么占cnt/k+1 ...
- C - Ekka Dokka
Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to sh ...
- 使用HTMLTestRunner生成报告
使用HTMLTestRunner生成报告 unittest本身并不具备这个功能,需要使用HTMLTestRunner库 使用步骤: 首先需要下载.py文件:http://tungwaiyip.info ...
- [转载]深度理解Session
什么是session session的官方定义是:Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息. 说白了session就是 ...
- asp.net core identity学习1
ASP.NET Identity 学习 创建一个Asp.net core mvc项目 添加Nuget包: Microsoft.EntityFrameworkCore.SqlServer 3.1.3 M ...
- windows 系统使用技巧
1 自定义发送到C:\Users\adm\AppData\Roaming\Microsoft\Windows\SendTo 2 自定义关机shutdown -s -t time,可写在快捷方式中shu ...
- Java中Character类
Character 类在对象中包装一个基本类型char的值此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等),并将字符从大写转小写,反之亦然. 构造方法: Character(char ...
- tensorflow1.0 构建lstm做图片分类
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #this is data mni ...
- 用python把技术文档中,每个模块系列截图生成一个动态GIF
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 最近在写技术文档的时候,发现一个问题.对于每个技术步骤,都需要一个截图,这 ...
- Python数据分析:大众点评数据进行选址
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:砂糖侠 如果你处于想学Python或者正在学习Python,Pyth ...