ZOJ3286

一顿打表。

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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值