2018南京区域赛——G - Pyramid
https://blog.csdn.net/m0_37624640/article/details/83276324
这篇博客很详细,也很容易理解。求每个的贡献贡献,得到ans[i]=(n−prime[i][pos]+1)∗(prime[i][pos]−prime[i][pos−1]);
#include<cstring> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cstdio> #include<stack> #include<cmath> #include<iostream> #define ll long long #define lowbit(x) x&(-x) using namespace std; const int INF=0x3f3f3f3f; const int maxn=1e6+7; int n,a[maxn]; vector<int> prime,pos[maxn]; bool vis[maxn]={false}; void dive(int x,int p) { for(int i=2;i<=sqrt(x);i++) { if(x%i==0) { pos[i].push_back(p); if(!vis[i]) { prime.push_back(i); vis[i]=true; } while(x%i==0) x/=i; } } if(x>1) { pos[x].push_back(p); if(!vis[x]) { prime.push_back(x); vis[x]=true; } } } int main() { for(int i=0;i<maxn;i++) pos[i].push_back(0); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); dive(a[i],i); } ll ans=0; for(int i=0;i<prime.size();i++) { for(int j=1;j<pos[prime[i]].size();j++) { ans+=(ll)(pos[prime[i]][j]-pos[prime[i]][j-1])*(ll)(n-pos[prime[i]][j]+1); } } printf("%lld\n",ans); return 0; }