题目:

题意:给出一个序列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. no need jQuery anymore & You don't need jQuery anymore!

    no need jQuery anymore & You don't need jQuery anymore! "use strict"; /** * * @author ...

  2. Regular Expression & rgb2hex

    Regular Expression & rgb2hex regex // 颜色字符串转换 function rgb2hex(sRGB = 'rgb(255, 255, 255)') { co ...

  3. HTTP/HTTPS Proxy & Charles

    HTTP/HTTPS Proxy & Charles Charles https://www.charlesproxy.com/ https://www.jianshu.com/p/53d2c ...

  4. Transporter iOS App

    Transporter iOS App apple store app store connect https://appstoreconnect.apple.com/ Transporter &am ...

  5. React Hooks & useCallback & useMemo

    React Hooks & useCallback & useMemo https://reactjs.org/docs/hooks-reference.html#usecallbac ...

  6. WEB 硬件设备联网

    在js中与第三方设备的通信 这是一个Xbox One手柄的demo,将手柄使用USB连接到PC上: See also: video Device APIs 通过JavaScript与蓝牙设备通信 在网 ...

  7. 揭秘高倍矿币 Baccarat BGV,为何NGK DeFi的财富效应如此神奇?

    作为区块链4.0代表的NGK公链,这次也将借助它自己的DeFi版块NGK Baccarat,开启属于它自己的千倍财富之旅. 如果说,比特币能让没有银行账户的人,可以在全球任何时间.地点都能自由进行交易 ...

  8. Redis的数据结构与应用场景

    一.Redis简介 Redis 是一个开源的使用 ANSI C 语言编写.遵守 BSD 协议.支持网络.可基于内存.分布式.可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API ...

  9. PBN进场程序保护区图例分析

      疫情仍在持续,除了待家里不给祖国添乱之外,过去没有时间去完成的事情,现在可以静下心来认真面对,充实过好每一天. 今天想跟大家聊一下ICAO 8168第二卷PBN进场程序的图例. 就是下面这张图: ...

  10. 谈谈 JS 垃圾回收机制

    谈谈 JS 垃圾回收机制 JS内存泄漏与垃圾回收机制 https://javascript.info/garbage-collection