HDU1215 七夕节(另一种思想)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1215

 

题目大意:

求所有正整数N的因子之和;

 

题目分析:

这题没什么难的,主要借此来说一下另一种别致的思想:

1)表面上看,我们眼中默认的整体是所有输入的数,默认的子问题是每次输入的数,默认的部分一个一个的找因数;

这样虽然我们站在的总体是一样的,但我们把整体分解成了子问题,我们实际的整体其实是每次输入的数!

 

2)换个角度,如果我们以全体输入的数为实际整体,不分解出子问题分别去解决,而是每次去解决整体问题的部分,

一点点累积,中间过程不产生任何一个1)中的子问题的解,而最后一步一下子累积出1)中所有子问题的解,从而解

决整体问题;这其实就是站在更宏观的角度看整体,和筛法的实现思想很像。

 

3)适用范围:可尝试与多个子问题的同时求解,每个子问题间应该有关联才行。

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;

#define CLR(arr, what) memset(arr, what, sizeof(arr))
const int N = 500002;
const int M = 250000;
int a[N] = {0, 0};

void fash()
{
	for(int i = 2; i < N; ++i) //因子初始化为1
		a[i] = 1;
	for(int i = 2; i <= M; ++i) //一半即可
		for(int j = 2 * i; j < N; j += i)
			a[j] += i;
}

int main()
{
	fash();
	int ncase;
	scanf("%d", &ncase);
	while(ncase--)
	{
		int num;
		scanf("%d", &num);
		printf("%d\n", a[num]);
	}
	return 0;
}



 

posted on 2013-09-06 00:31  Gddxz  阅读(232)  评论(0编辑  收藏  举报

导航