码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces-546D Soldier and Number Game 筛法+动态规划

时间:2018-09-04 22:32:57      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:lse   tps   memory   scanf   clu   amp   div   现在   gnu   

题目链接:https://cn.vjudge.net/problem/CodeForces-546D

题意

抱歉,我给忘了,现在看题目又看不懂: P

思路

筛法+dp
话说这个函数应该是积性函数,然后就想到了动态规划优化筛法。

提交过程

AC

代码

#include <cstdio>
#include <cstring>
const int maxp=1e6, maxn=5e6+20;
int primes[maxn], psize;
long long ans[maxn], pre[maxn];
bool isprime[maxn];
void div(void){
    memset(isprime, true, sizeof(isprime));
    for (int i=0; i<maxn; i++)
        pre[i]=i;
    for (int i=2; i<maxn; i++){
        if (isprime[i]){
            ans[i]=1;
            for (int j=2; j*i<maxn; j++){
                isprime[j*i]=false;
                ans[j*i]++; pre[j*i]/=i;
            }
        }else ans[i]+=ans[pre[i]];
    }
    for (int i=1; i<maxn; i++)
        ans[i]+=ans[i-1];
}

int main(void){
    int t, a, b;

    div();
    scanf("%d", &t);
    while (t--){
        scanf("%d%d", &a, &b);
        printf("%lld\n", ans[a]-ans[b]);
    }

    return 0;
}
Time Memory Length Lang Submitted
1247ms 102564kB 747 GNU G++ 5.1.0 2018-08-31 10:13:44

CodeForces-546D Soldier and Number Game 筛法+动态规划

标签:lse   tps   memory   scanf   clu   amp   div   现在   gnu   

原文地址:https://www.cnblogs.com/tanglizi/p/9588834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!