HDU 5525 Product
来源:互联网 发布:内向工作知乎 编辑:程序博客网 时间:2024/05/19 00:41
首先 4可以转化为2²这样 即对于 1 2 3 4 5 6 这样的输入 可以转化为 1 16 9 0 1 0 这样 即表示成一些素数的k次方的乘积的形式
假设N=2²3³5³ 则N的因子 可以通过枚举取几个2,3,5 来计算出来 由于要计算所有 因子的乘积
所以当我们考虑2这一维时 其他的数一共只有(3+1)*(3+1) 16 种取法 而2这一维可以取1,2,2²这三种 所以2对于结果的影响f(2)=2的((0+1+2)*16) 次方
讲所有的f(p)乘起来即可 由于计算幂的时候要取模 而取模的又是指数 所以要模mod-1(即euler(mod)) 而计算2本身时的个数时 应该是num[2]*(num[2]+1)/2 %(mod-1)
这里先做乘法可能会爆longlong 而先取模可能导致 无法除2 所以可以先特判 然后除2 然后再取模 也可以先%(2*mod) 再相乘除2 再模mod
而对于某一个数 其他数的选取总方案数 用了两个数组前缀积和后缀积来维护 从而避免了除法的操作 (因为1e9+6是合数 所以不能求逆)
#include<iostream>#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#include<vector>#include<queue>#include<map>#include<algorithm>#include<set>#define scnaf scanf#define cahr char#define bug puts("=========================");using namespace std;typedef long long ll;const int maxn=1e5+5;const int mod=1000000007;const int mod2=1000000006;//几乎线性的速度const int MAXP=maxn+5;bool is_prime[MAXP];int prime[MAXP/5],np;void init_prim(int n){ memset(is_prime, true, sizeof(is_prime)); int first = 1; np = first; for (int i = 2; i <= n; i++){ if (is_prime[i]) prime[np++] = i; for (int j = first; ((j < np) && (i * prime[j] <= n)); ++j){ is_prime[i * prime[j]] = 0; if (i % prime[j] == 0) break; //点睛之笔 } }}vector<int>vec[maxn];void init(){ for(int i=2;i<maxn;i++) { int now=i; for(int j=1;prime[j]*prime[j]<=now;j++) while(now%prime[j]==0) { vec[i].push_back(prime[j]); now/=prime[j]; } if(now!=1) vec[i].push_back(now); }}ll a[maxn];ll numl[maxn],numr[maxn];ll powmod(ll a,ll b){ ll res=1; while(b) { if(b&1) res=res*a%mod; a=a*a%mod; b/=2; } return res;}ll mul(ll x){ ll a=x; ll b=x+1; if(a&1) b/=2; else a/=2; return (a%mod2)*(b%mod2)%mod2;}int main(){ init_prim(MAXP-5); init(); int n; while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { int z; scanf("%d",&z); for(int j=0;j<vec[i].size();j++) a[vec[i][j]]+=z; } int cnt=1; numl[0]=1; for( ;prime[cnt]<=n;cnt++){ numl[cnt]=numl[cnt-1]*(a[prime[cnt]]+1)%mod2; } numr[cnt--]=1; for(;cnt>0;cnt--){ numr[cnt]=numr[cnt+1]*(a[prime[cnt]]+1)%mod2; } ll ans=1; for(int i=1;prime[i]<=n;i++) if(a[prime[i]]) { ll num=numl[i-1]*numr[i+1]%mod2; // cout<<numl[i-1]<<" "<<numr[i+1]<<endl; ll x=powmod(prime[i],mul(a[prime[i]])*num%mod2)%mod; ans=ans*x%mod; } printf("%I64d\n",ans); }}
0 0
- HDU 5525 Product
- HDU 5525 Product
- HDU 5525 Product(数论)
- HDU 5525 Product
- hdu 5525 Product(数论)
- HDU 5525(Product-费马小定理)
- HDU 5525:Product 欧拉定理
- hdu-5524 Product
- hdu 5525 Product (费马小定理优化的快速幂)
- HDU 1070 - Select the cheapest product
- product
- Product
- Product
- Product
- Product
- hdu 5453 Dividing This Product(分块打表)
- 【HDU】5757 Product Bo【分情况讨论+队列】
- hdoj 5525 Product 【组合数学+质因子处理】
- 【黑马程序员】方法与数组
- 关于xcode7升级以后,无法archive,或者archive后xcode闪退的问题处理方案
- 【LeetCode OJ 007】Reverse Integer
- ProgressBar样式的定义
- LeetCode H-Index II 数组+二分
- HDU 5525 Product
- SQL Server 2005中的except/intersect和outer apply
- 杭电1166敌兵布阵
- JS下拉列表,随着省份的改变,城市也会有所改变
- 最基础的MarkDown使用
- DDL、DML和DCL的理解
- 分享一个VBA连接mysql数据库的方法
- 有重复元素的排列问题
- .net之jquery操作gridview中的数据