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次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- 关于自定义的 XIB cell上的 button如何在控制器里实现点击方法
直接调用cell.button addTarget 的方法点击事件是失效的 这时需要你在xib中设置button的tag值 然后在返回cell的时候添加点击事件 UIButton *button = ...
- beautifulsoup 获取a(tag)的属性href
一开始使用使用attrs(“href”) 出现错误TypeError: 'dict' object is not callable 由于attrs字典类型 atrrs["href" ...
- MAC上配置asp.net core开发环境
安装.NET Core sdk https://www.microsoft.com/net/core#macos 安装VS Code https://code.visualstudio.com/Dow ...
- [DB] - Mysql创建定时任务
mysql支持定时任务的创建,要求mysql服务器开始定时任务调度. 1. 查看是否开启定时任务执行 SHOW VARIABLES LIKE 'event_scheduler'; // OFF表示没有 ...
- linux下常用语言的语法检查插件整理
linux下常用语言的语法检查插件 可以结合vim语法检查插件syntastic使用,具体请参考syntastic使用说明 如php,sql,json,css,js,html,shell,c等语法插件 ...
- java中final小结
fanal 修饰类,该变量一经赋值,就不能够再修改 修饰类,该类不能让子类继承. 修饰方法,该方法不能被子类重写(隐藏). fanal修饰类与方法的意义 1 某个类或方法实现上已经非常完善,不需要子 ...
- struts2接收参数的5种方法
以下形式中最常用的是前两种 1. 使用Action的属性: 在action 里面定义要接收的参数,并提供相应的setter,getter,和提交参数的名称一致, 并不用做数据类型的转换相应提交方式可以 ...
- 了解 : Odata 的 $filter
api/jobPosts?$filter=company/name eq "string" //基本 api/orders?$filter=orderItem/product/EF ...
- C# 字符串比较大小 string.Compare()方法
string.Compare方法,用来比较2个字符串值得大小 string.Compare(str1, str2, true); 返回值: 1 : str1大于str2 0 : str1等于str2 ...
- 【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则
作者:Williammao, 腾讯移动客户端开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/290.h ...