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

题目链接: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

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

时间: 2024-07-30 13:25:57

CodeForces-546D Soldier and Number Game 筛法+动态规划的相关文章

Codeforces 546D Soldier and Number Game(数论)

类似筛素数的方法--求出前缀和.然后直接O(1)回答即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 7 const int N = 10000000 + 10; 8 9 int num[N]; 10 bool p[N]; 11 int T, n, m; 12 13 int main(){ 14 15 memset(p,

codeforces 546D Soldier and Number Game

题目链接 这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以. 具体看代码, 代码里面有解释 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 const int maxn = 5000005; 5 int p[maxn], c[maxn]; 6 int main() 7 { 8 memset(p, 0, sizeof(p)); 9 memset(

CodeForces 546D Soldier and Number Game 打表(求质因子个数)

题目:戳我 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/b!); 分析:由于这里1 ≤ b ≤ a ≤ 5 000 000,数据很大,所以简单暴力一定超时,具体看代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int M = 5e6+5; 5 6 int t; //测试组数 7

CF 546D Soldier and Number Game

Describe Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive

Codefo 546D. Soldier and Number Game

题目链接:http://codeforces.com/problemset/problem/546/D 题意:输入a,b,n=a!/b!,求n 最多除以多少次变为1. 分析:相当于求n有多少个质因子.即求从b+1到a之间的数字质因子和为多少. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int v[5000005];//v[i]记录i是否为质数 int pri

DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

Codeforces 464C Substitutes in Number(高效+快速幂)

题目链接:Codeforces 464C Substitutes in Number 题目大意:给定一个字符串,以及n中变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一 个数,取模1e9+7. 解题思路:将操作倒过来处理,这样维护每个数来的val,len两个,val表示对应数值取模1e9+7,len表示对应有多少 位,再计算的过程中要使用. #include <cstdio> #include <cstring> #include <vector>

CodeForces 546D (求素因子个数)

/***************************** CodeForces 546D Author:herongwei Created Time: 2016/5/31 13:00:00 File Name : main.cpp 给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n, 最后当n变成1的时候经过了几轮得分就是这个轮数, 要求最大的分数是多少 [solve] 就是一个求整数质因子个数的题目, 阶乘我们不需要算,我们知道在a>b的时候, b!都约掉了,那么我们只需计算出