题目:

题意:给出一个序列a1,⋯,ana1,⋯,an。fac(l,r)fac(l,r)为mul(l,r)mul(l,r)中不同质因数的个数。

请计算:

                ∑i=1n∑j=infac(l,r)
思路:求质因数的贡献度,我们可以定义一个二维vector pos[i][k]=p表示当前质因数i在p位置出现k次那么该因子的贡献度为(n-p+1)*p,
           因为可能会重复计算的,那么我们只要减去上一个位置出现i的情况,那么每一个质因子的贡献度为(n-pos[i][k]+1)*(pos[i][k]-pos[i][k-1];
           有两种实现一种直接质数分解,大概跑了800ms,用一个欧拉筛进行打表优化跑了400ms。

  1 //#include<bits/stdc++.h>
2 #include<time.h>
3 #include <set>
4 #include <map>
5 #include <stack>
6 #include <cmath>
7 #include <queue>
8 #include <cstdio>
9 #include <cstring>
10 #include <string>
11 #include <vector>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #include <list>
16 using namespace std;
17 #define eps 1e-10
18 #define PI acos(-1.0)
19 #define lowbit(x) ((x)&(-x))
20 #define zero(x) (((x)>0?(x):-(x))<eps)
21 #define mem(s,n) memset(s,n,sizeof s);
22 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
23 typedef long long ll;
24 typedef unsigned long long ull;
25 const int maxn=1e6+5;
26 const int Inf=0x7f7f7f7f;
27 const ll Mod=1e9+7;
28 const int N=3e3+5;
29 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
30 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
31 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
32 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
33 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
34 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
35 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
36 int Abs(int n) {
37 return (n ^ (n >> 31)) - (n >> 31);
38 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
39 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
40 需要计算 n 和 -1 的补码,然后进行异或运算,
41 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
42 }
43 ll binpow(ll a, ll b,ll c) {
44 ll res = 1;
45 while (b > 0) {
46 if (b & 1) res = res * a%c;
47 a = a * a%c;
48 b >>= 1;
49 }
50 return res%c;
51 }
52 void extend_gcd(ll a,ll b,ll &x,ll &y)
53 {
54 if(b==0) {
55 x=1,y=0;
56 return;
57 }
58 extend_gcd(b,a%b,x,y);
59 ll tmp=x;
60 x=y;
61 y=tmp-(a/b)*y;
62 }
63 ll mod_inverse(ll a,ll m)
64 {
65 ll x,y;
66 extend_gcd(a,m,x,y);
67 return (m+x%m)%m;
68 }
69 ll eulor(ll x)
70 {
71 ll cnt=x;
72 ll ma=sqrt(x);
73 for(int i=2;i<=ma;i++)
74 {
75 if(x%i==0) cnt=cnt/i*(i-1);
76 while(x%i==0) x/=i;
77 }
78 if(x>1) cnt=cnt/x*(x-1);
79 return cnt;
80 }
81 int n,a[maxn];
82 vector<int>pos[maxn];
83 int cnt=0;
84 bool isprime[maxn];
85 int prime[maxn];
86 void judge()
87 {
88 cnt=0;
89 mem(isprime,1);
90 isprime[1]=0;
91 for(int i=2;i<maxn;i++)
92 {
93 if(isprime[i])
94 prime[cnt++]=i;
95 for(int j=0;j<cnt&&(i*prime[j])<maxn;j++)
96 {
97 isprime[i*prime[j]]=0;
98 if(i%prime[j]==0) break;
99 }
100 }
101 }
102 void dec(int p)
103 {
104 int n=a[p];
105 for(int i=0;i<cnt&&prime[i]*prime[i]<=n;i++)
106 {
107 if(n%prime[i]==0)
108 {
109 pos[prime[i]].push_back(p);
110 while(n%prime[i]==0) n/=prime[i];
111 }
112 }
113 if(n>1) pos[n].push_back(p);
114 }
115 void dec_(int p)
116 {
117 int n=a[p];
118 for(int i=2;i*i<=n;i++)
119 {
120 if(n%i==0)
121 {
122 pos[i].push_back(p);
123 while(n%i==0) n/=i;
124 }
125 }
126 if(n>1) pos[n].push_back(p);
127 }
128 int main()
129 {
130 judge();
131 for(int i=0;i<cnt;i++) pos[prime[i]].push_back(0);
132 scanf("%d",&n);
133 for(int i=1;i<=n;i++)
134 {
135 scanf("%d",&a[i]);
136 dec(i);
137 }
138 ll ans=0;
139 for(int i=0;i<cnt;i++)
140 {
141 for(std::size_t k=1;k<pos[prime[i]].size();k++)
142 {
143 ans+=(ll)(n-pos[prime[i]][k]+1)*(pos[prime[i]][k]-pos[prime[i]][k-1]);
144 }
145 }
146 printf("%lld\n",ans);
147 return 0;
148 }
 

2018ICPC 南京Problem J. Prime Game的更多相关文章

  1. 2018ICPC南京站Problem J. Prime Game

    题意: 对于所有数字分解质因子,如果某个质因子在这个区间出现,则贡献为1,求所有质因子对所有区间做的贡献. 解析: 考虑如果所有全部区间都有这个质因子则这个质因子的贡献是n*(n+1)/2,对于任意因 ...

  2. 2018ICPC南京Problem G. Pyramid

    题意: 询问类似于这样的三角形中:里面正三角形的个数是多少. 思路:打表找了个规律发现就是C4n+3     1 //#include<bits/stdc++.h> 2 #include& ...

  3. 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...

  4. 2018ICPC南京网络赛

    2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...

  5. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  6. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  7. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  8. 素数筛法--SPOJ Problem 2 Prime Generator

    质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单, ...

  9. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

随机推荐

  1. Springboot如何启用文件上传功能

    网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...

  2. Vue & Sentry & ErrorHandler

    Vue & Sentry & ErrorHandler import * as Sentry from '@sentry/browser'; import { Vue as VueIn ...

  3. Apple Screen Recorder All In One

    Apple Screen Recorder All In One Apple macOS 自带录屏 QuickTime Player https://support.apple.com/zh-cn/g ...

  4. GitHub rename the default branch from master to main

    GitHub rename the default branch from master to main master => main Repository default branch Cho ...

  5. How to get a DOM element's ::before content with JavaScript?

    How to get a DOM element's ::before content with JavaScript? https://stackoverflow.com/questions/443 ...

  6. qrcode & console.log

    qrcode & console.log image https://fs-api.lightyy.com/service/utils/qrcode?url=http://169.254.13 ...

  7. Union international INC:VR线下娱乐市场巨大

    联合国际公司认为众多企业追着VR的风口不断加码,导致VR在经历了一个爆炸式的发展,如今部分VR公司开始觉得日子不好过了,一个重要表现就是现金流紧张.VR如何能够普及,何时能够及早变现,成为业内关注的焦 ...

  8. JPEG解码——(4)霍夫曼解码

    本篇是该系列的第四篇,主要介绍霍夫曼解码相关内容. 承接上篇,文件头解析完毕后,就进入了编码数据区域,即SOS的tag后的区域,也是图片数据量的大头所在. 1. 解码过程规则描述 a)从此颜色分量单元 ...

  9. 关于Java中的对象、类、抽象类、接口、继承之间的联系

    关于Java中的对象.类.抽象类.接口.继承之间的联系: 导读: 寒假学习JavaSE基础,其中的概念属实比较多,关联性也比较大,再次将相关的知识点复习一些,并理顺其中的关系. 正文: 举个例子:如果 ...

  10. 痞子衡嵌入式:系统时钟配置不当会导致i.MXRT1xxx系列下OTFAD加密启动失败

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是系统时钟配置不当会导致i.MXRT1xxx系列下OTFAD加密启动失败问题. 我们知道,i.MXRT1xxx家族早期型号(RT1050/ ...