The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564
地址: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 ;
}
The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564的更多相关文章
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...
- The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554
地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others) M ...
- The 15th UESTC Programming Contest Preliminary B - B0n0 Path cdoj1559
地址:http://acm.uestc.edu.cn/#/problem/show/1559 题目: B0n0 Path Time Limit: 1500/500MS (Java/Others) ...
- The 15th UESTC Programming Contest Preliminary K - Kidd1ng Me? cdoj1565
地址:http://acm.uestc.edu.cn/#/problem/show/1565 题目: Kidd1ng Me? Time Limit: 3000/1000MS (Java/Others) ...
- The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557
地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ...
- The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551
地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...
- The 15th UESTC Programming Contest Preliminary D - Destr0y City cdoj1558
地址:http://acm.uestc.edu.cn/#/problem/show/1558 题目: D - Destr0y City Time Limit: 3000/1000MS (Java/Ot ...
- 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken
题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ...
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- C++虚表(V-Table)解析
C++中的虚函数的作用主要是实现了多态,本人通过代码验证的方式了解虚表的结构及在多种继承方式下通过虚表访问子类函数.验证结果如下: 1)无虚函数覆盖的一般继承:可以通过子类的虚表访问父类的函数 2)虚 ...
- Ansible之 Inventory 资源清单介绍
一.Inventory 库存清单文件 1.Inventory 作用 Ansible 可以在同一时间针对多个系统设施进行管理工作.它通过选择Ansible 资源清单文件中列出的系统,该清单文件默认是在/ ...
- shell-3
每天学习一篇:利用跑代码或者空闲思考时间,启动环境时间等闲杂时间: http://blog.csdn.net/junjieguo/article/category/880326/2
- hibernate系列笔记(1)---Hibernate增删改查
Hibernate增删改查 1.首先我们要知道什么是Hibernate Hibernate是一个轻量级的ORMapping对象.主要用来实现Java和数据库表之间的映射,除此之外还提供数据查询和数据获 ...
- 解决新建maven项目速度慢的问题
问题描述 通过idea新建maven项目,参数设置好后,idea自动构建maven项目时,速度很慢. 参数设置如图: 执行时间如下图: Total time为8:49,花了将近十分钟时间. 连续尝试了 ...
- Git 基本工作流程
1. 用户信息配置(全局配置) $ git config --global user.name leo$ git config --global user.email hehe_xiao@qq.com ...
- JS入门(四)
接之前一篇的函数.写之前的函数的时候讲的比较笼统,在这重新写一下函数的内容. 函数: 之前提过,函数就是代码复用的一种机制或是将代码封装成功能的代码段.函数的声明在这边就不多提了,因为相对来说比较简单 ...
- .NET 三层架构
三层架构简介: 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer).业务逻辑层(Business Lo ...
- 阿里宣布Atlas正式开源:带你重返App开发的田园时代
继Weex之后,阿里在移动技术领域又有开源大动作. 3月13日,手机淘宝安卓客户端容器化框架Atlas正式宣布开源(https://github.com/alibaba/atlas ).Atlas由阿 ...
- wemall app商城源码Android之支付宝通知处理类
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之处 ...