地址:http://acm.uestc.edu.cn/#/problem/show/1564
题目:
G - GC?(X,Y)
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131071/131071KB (Java/Others)
One positive integer can be represented by the product of some prime numbers.
Sort the prime numbers, such like 60=2∗2∗3∗560=2∗2∗3∗5, 180=2∗2∗3∗3∗5180=2∗2∗3∗3∗5.
The GCPGCP(Greatest Common Prefix) of two positive integers is defined as the longest prefix of the multiplication of sorted prime numbers.
For example, GCP(60,180)=Longest_Prefix(2∗2∗3∗5,2∗2∗3∗3∗5)=2∗2∗3=12GCP(60,180)=Longest_Prefix(2∗2∗3∗5,2∗2∗3∗3∗5)=2∗2∗3=12.
Now, for a given array AiAi, calculate
.
Input
The first line contains a number NN.
The second line contains NN integers AiAi.
1≤N≤105,1≤Ai≤1071≤N≤105,1≤Ai≤107
Output
Output the sum described above.
Sample input and output
Sample Input | Sample Output |
---|---|
5 |
13 |
Hint
In the sample,
GCP(1,2)=GCP(1,8)=GCP(1,5)=GCP(1,10)=GCP(2,5)=GCP(8,5)=GCP(5,10)=1GCP(1,2)=GCP(1,8)=GCP(1,5)=GCP(1,10)=GCP(2,5)=GCP(8,5)=GCP(5,10)=1,
GCP(2,8)=GCP(2,10)=GCP(8,10)=2GCP(2,8)=GCP(2,10)=GCP(8,10)=2.
时间复杂度:O(nlogn+25nlogn)
3.哈希前缀
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef long long LL;
const int K=1e5+;
const int maxn=1e7+;
vector<int>num[K],va;
vector<LL>vb;
int n,pri[maxn],v[K],tol,tag[maxn];
LL ans;
bool cmp(const vector<int> &ta,const vector<int> &tb)
{
for(int i=,j=min(ta.size(),tb.size());i<j;i++)
if(ta[i]!=tb[i]) return ta[i]<tb[i];
return ta.size()<tb.size();
}
void init(void)
{
for (int i = ; i * i < maxn; i++)
{
if (tag[i]) continue;
for (int j = i; j * j < maxn; j++)
tag[i*j] = ;
}
for (int i = ; i < maxn; i++)
if (!tag[i])
pri[tol++] = i;
for(int i=; i<n; i++)
{
int t=v[i];
num[i].PB();
for(int j=; pri[j]*pri[j]<=t; j++)
{
if(v[i]%pri[j]) continue;
while(v[i]%pri[j]==) v[i]/=pri[j],num[i].PB(pri[j]);
}
if(v[i]>) num[i].PB(v[i]);
}
}
void bs(int x)
{
int sum=;
int l,r,mid,tmp=n-;
va.clear(),vb.clear();
for(int i=;i<num[x].size();i++)
{
l=x,r=tmp;
while(l<=r)
{
mid=(l+r)/;
if(num[mid].size()>i&&num[mid][i]==num[x][i])
tmp=mid,l=mid+;
else
r=mid-;
}
sum*=num[x][i];
va.PB(tmp),vb.PB(sum);
}
for(int i=num[x].size()-,ls=x;i>=;i--)
ans+=vb[i]*(va[i]-ls),ls=va[i];
}
int main(void)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",v+i);
init();
sort(num,num+n,cmp);
for(int i=;i<n;i++)
bs(i);
cout<<ans<<endl;
return ;
}
动态分配内存的trie树,未ac,mle16了,不想改成左儿子右兄弟了:
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
#define MAXNUM 27500
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+;
const int maxn=1e7+; vector<int>num[K];
map<int,int>hs;
int n,pri[maxn],v[K],tol,tag[maxn];
int ths[K];
LL ans;
void make_prime()
{
for (int i = ; i * i < maxn; i++)
{
if (tag[i])
continue;
for (int j = i; j * j < maxn; j++)
tag[i*j] = ;
}
for (int i = ; i < maxn; i++)
if (!tag[i])
pri[tol++] = i;
}
void init()
{
for(int i=; i<n; i++)
{
int t=v[i];
num[i].PB();
for(int j=; pri[j]*pri[j]<=t; j++)
{
if(v[i]%pri[j]) continue;
while(v[i]%pri[j]==) v[i]/=pri[j],num[i].PB(pri[j]);
}
if(v[i]>) num[i].push_back(v[i]);
}
}
typedef struct Trie
{
int sum,ed;
Trie *next[MAXNUM];
}Trie;
Trie *root;
void TrieInit(int sz)
{
root = (Trie *)malloc(sizeof(Trie));
root->sum=root->ed=;
for(int i=;i<sz;i++)
root->next[i]=NULL;
for(int j=;j<n;j++)
{
Trie *tem=root;
for(int i=;i<num[j].size();i++)
{
//printf("x=%d:%d %d\n",j,num[j][i],hs[num[j][i]]);
if(tem->next[hs[num[j][i]]]==NULL)
{
Trie *cur = (Trie *)malloc(sizeof(Trie));
for(int k=;k<sz;k++)
cur->next[k]=NULL;
cur->sum=cur->ed=;
tem->next[hs[num[j][i]]]=cur;
}
tem = tem->next[hs[num[j][i]]];
tem->ed++;
}
tem->sum++;
}
}
void dfs(Trie *x,LL y,int sz)
{
Trie *tem = x,*cur;
LL ta=,tb=,ff=;
if(tem->ed-tem->sum>)
for(int i=;i<sz;i++)
if(tem->next[i]!=NULL)
{
cur=tem->next[i],ff++;
tb+=ta*cur->ed;
ta+=cur->ed;
if(cur->ed)dfs(cur,y*ths[i],sz);
}
if(ff>||tem->sum>)
ans+=y*tb+y*tem->sum*(tem->ed-tem->sum)+y*tem->sum*(tem->sum-)/2LL; } int main(void)
{
int cnt=;
scanf("%d",&n);
make_prime();
for(int i=;i<n;i++)
scanf("%d",v+i);
init();
for(int i=;i<n;i++)
{
//printf("x=%d: ",i);
for(int j=;j<num[i].size();j++,cnt++)
//printf("%d ",num[i][j]),
ths[cnt]=num[i][j];
//printf("\n");
}
sort(ths,ths+cnt);
int sz=unique(ths,ths+cnt)-ths;
for(int i=;i<sz;i++)
hs[ths[i]]=i;
TrieInit(sz);
for(int i=;i<sz;i++)
if(root->next[i]!=NULL)
dfs(root->next[i],ths[i],sz);
cout<<ans<<endl;
return ;
}