【杂题】 HDOJ 4961 Boring Sum

预处理约数就可以了。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 40005
#define eps 1e-10
#define mod 998244353
#define INF 999999999
#define lowbit(x) (x&(-x))
#define mp mark_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R
//#pragma comment (linker,"/STACK:102400000,102400000")
typedef long long LL;
//typedef int LL;
using namespace std;
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
// head

vector<int> g[maxn];
int a[maxn], next[maxn];
int b[maxn], c[maxn];
int n;

void init(void)
{
	int tmp;
	for(int i = 1; i <= 100000; i++) {
		tmp = sqrt(i);
		for(int j = 1; j <= tmp; j++)
			if(i%j == 0) {
				g[i].push_back(j);
				if(j != i/j)
					g[i].push_back(i/j);
			}
	}
}
void work(void)
{
	LL ans = 0;
	memset(next, -1, sizeof next);
	for(int i = 1; i <= n; i++) {
		if(next[a[i]] == -1) b[i] = a[i];
		else b[i] = a[next[a[i]]];
		int d = g[a[i]].size();
		for(int j = 0; j < d; j++) next[g[a[i]][j]] = i;
	}
	memset(next, -1, sizeof next);
	for(int i = n; i >= 1; i--) {
		if(next[a[i]] == -1) c[i] = a[i];
		else c[i] = a[next[a[i]]];
		int d = g[a[i]].size();
		for(int j = 0; j < d; j++) next[g[a[i]][j]] = i;
	}
	for(int i = 1; i <= n; i++) ans += (LL)b[i]*c[i];
	printf("%I64d\n", ans);
}
int main(void)
{
	init()
	while(scanf("%d", &n), n != 0) {
		for(int i = 1; i <= n; i++) scanf(a[i]);//scanf("%d", &a[i]);
		work();
	}
	return 0;
}


你可能感兴趣的:(HDU)