Sicily 13291. Play with Numbers
2015-03-17 00:24
337 查看
13291. Play with Numbers
Constraints
Time Limit: 5 secs, Memory Limit: 256 MBDescription
In number theory, an integer a is a multiple of an integer b if a = kb for some integer k.Given a sequence a1, a2, …, an, let S(i) = {j | 1<=j<i, and aj is a multiple of ai}. IfS(i) is not empty,
let f(i) be the maximum integer in S(i); otherwise, f(i) = i. Now we define bi as af(i). Similarly, let T(i) = {j | i<j<=n,
and aj is a multiple of ai}. If T(i) is not empty, let g(i) be the minimum integer in T(i); otherwise, g(i) = i. Now we define ci as ag(i).
Your task is to calculate b1 * c1 + b2 * c2 + … + bn * cn.
Input
The input consists of two lines. The first line contains an integer n(1<=n<=100000). The second line contains n integers a1, a2, …, an (1<=ai<=100000).Output
Output the answer in a line.Sample Input
51 4 2 3 9
Sample Output
136
Hint
In the sample, b1=1, c1=4, b2=4, c2=4, b3=4, c3=2, b4=3, c4=9, b5=9, c5=9,so b1 * c1 + b2 * c2 + … + b5 * c5 = 136.
Problem Source
"6CIT杯"第五届中山大学ICPC新手赛 by 黄嘉星// Problem#: 13291 // Submission#: 3523793 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <stdio.h> #include <math.h> int prime[10000]; bool isPrime[100005]; void getPrime( int x ){ for ( int i=1; i<x; i+=2 ) isPrime[i] = 1, isPrime[i-1] = 0; prime[prime[0]=1 ] = 2; for ( int i=3; ; i+=2 ) if(isPrime[i]) { int j = i*i, k = i+i; if(j >= x) break; while(j < x ) { isPrime[j] = 0; j += k; } } for ( int i=3; i<x; i += 2 ) if(isPrime[i]) prime[++prime[0] ] = i; } int p[10000], cnt[10000]; inline void getPrimeDivisor(int x) { p[0] = cnt[0] = 0; int t; for ( int i=1; prime[i] * prime[i] <= x && i<=prime[0]; ++i ) { t = 0; while( x%prime[i] == 0 ) { ++t; x /= prime[i]; } if( t ) p[++p[0] ] = prime[i], cnt[++cnt[0] ] = t; } if(x > 1) p[++p[0] ] = x, cnt[++cnt[0] ] = 1; } int divisor[100005]; inline void getDivisor( int x ) { getPrimeDivisor(x); divisor[0] = 1; divisor[1] = 1; for ( int i=1; i<=p[0]; ++i ) { int nowNum = divisor[0]; int base = 1; for ( int j=1; j<=cnt[i]; ++j ) { base *= p[i]; for ( int k=1; k<=divisor[0]; ++k ) divisor[++nowNum ] = divisor[k]*base; } divisor[0] = nowNum; } } int num[100005]; int multiple[100005]; long long partOfSum[100005]; inline void update(int m) { if (isPrime[m]) { multiple[1] = m; multiple[m] = m; return; } getDivisor(m); for (int i = 1; i <= divisor[0]; i++) multiple[divisor[i]] = m; } void input() { // quickly input char text[800000]; gets(text); int i = 1; int sum = 0; for (int j = 0; text[j] != '\0'; j++) { if (text[j] == ' ') { num[i++] = sum; sum = 0; } else { sum = sum * 10 + text[j] - '0'; } } num[i] = sum; } int main() { getPrime(100005); int N; scanf("%d\n", &N); input(); for (int i = 1; i <= N; i++) multiple[num[i]] = num[i]; for (int i = 1; i <= N; i++) { partOfSum[i] = multiple[num[i]]; update(num[i]); } unsigned long long ans = 0; for (int i = 1; i <= N; i++) multiple[num[i]] = num[i]; for (int i = N; i > 0; i--) { ans += partOfSum[i] * multiple[num[i]]; update(num[i]); } printf("%llu\n", ans); return 0; }
相关文章推荐
- Sicily 13290. Play with Strings
- Improved App Insight by Linking Google Analytics with Google Play
- 【LeetCode with Python】 Add Two Numbers
- bzoj4353: Play with tree
- Play with libvirt
- UVA 10673 Play with Floor and Ceil
- PAT 1023. Have Fun with Numbers
- Codeforces Round #155 (Div. 2) A. Cards with Numbers
- pat甲 1023. Have Fun with Numbers(模拟+set)
- HDU 3487 Play with Chain(区间FLIP、CUT)
- 【PAT Advanced Level】1023. Have Fun with Numbers (20)
- 使用Matlab与V-REP联合仿真 - Play V-REP with Matlab
- Have Fun with Numbers
- uva10673 - Play with Floor and Ceil 扩展欧几里德算法
- PAT 1023. Have Fun with Numbers (20)(字符串转换,20位数的加法)
- [PTA MOOC] 自测-4 Have Fun with Numbers(20 分)
- AVI Player with Playmaker actions 视频转化格式之Duplicate frame remover(四)
- hdu 3487 Play with Chain(Splay)
- 【PAT甲级】1023. Have Fun with Numbers (20)
- Have Fun with Numbers(大数相加)