プロジェクトオイラー問204

http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20204
Problem 204 「一般化ハミング数」 †
ハミング数とは, どの素因数も5以下であるような正整数のことである. 最初から順に並べると, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15となる. 10^8以下のハミング数は1105個ある.

素因数がn以下の正整数を, type nの一般化ハミング数と呼ぶことにする. するとハミング数はtype 5の一般化ハミング数である.

10^9以下のtype 100の一般化ハミング数の個数を答えよ.

どう計算しても一秒以内に計算が終わります。
計算時間0.04秒

#include<stdio.h>
#include<iostream>
#include<map>

int LIMIT=10000*10000*10;
int ps[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};

int main(){
 	std::map<int,int> ms;
	std::map<int,int>::iterator it;
	ms[LIMIT]=1;
	for(int i=0;i<25;i++){
		int p=ps[i];
		for(it=ms.end(),it--;;it--){
			int f=(*it).first;
 			int s=(*it).second;
			if(f<p)break;
			ms[f/p]+=s;
			it=ms.find(f);
		}
	}
	int ans=0;
	for(it=ms.begin();it!=ms.end();it++){
		ans+=(*it).second;
	}
	printf("%d\n",ans);
}
最終更新:2016年01月24日 04:36