一顿打表。
zoj上最好的时间是50ms,最好的内存是176KB,好强啊。
自己和网上一些做法比较,重用了table数组,可以省掉一些内存。
count最大数可以通过构建过程中求出来是5,所以这个表可以小很多。
里面比较怪的是,把for循环里的i*j转成加法:
for(u64 j=i;j<numMax;j+=i) { table[j].... }
反而速度从390ms跳到620ms...
这个最后是390ms,4084KB run time memory:
#include <stdio.h> #include <algorithm> #include <math.h> typedef unsigned long u32; typedef unsigned short u16; typedef unsigned long long u64; typedef unsigned int uint; using namespace std; //------------code--------------------------------------------------------- const u64 maxNum = 1000000; u32 table[maxNum]; u32 countTable[maxNum]; void ConstructTable() { for(u64 i=1;i<=maxNum;i++) { const u64 top = maxNum/i; for(u64 j=1;j<=top;j++) { table[i*j]++; } } for(u64 i=1;i<=maxNum;i++) { u64 count = table[i]; //reuse the table as the result table,which will reduce memory a lot table[i] = countTable[count]; countTable[count]++; } } int main() { ConstructTable(); u64 num; while(scanf("%d",&num)!=EOF) { printf("%d/n",table[num]); } return 1; }