light_oj 1197 区间素数筛

本文介绍了一种解决区间素数计数问题的方法,通过预处理生成素数表,然后利用该表快速计算给定区间的素数数量。此算法在时间效率上有显著提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

light_oj 1197 区间素数筛

M - Help Hanzo
Time Limit:2000MS      Memory Limit:32768KB      64bit IO Format:%lld & %llu

Description

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.

Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.

He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).

Output

For each case, print the case number and the number of safe territories.

Sample Input

3

2 36

3 73

3 11

Sample Output

Case 1: 11

Case 2: 20

Case 3: 4

题意:给定a,b,求区间[a,b]中的素数个数。

思路:先常规的素数筛法筛出[1,10^6]的素数,再根据[1,10^6]的素数筛出[a,b]中的素数。

一开始用map超时了,改成set也超时了。。。最后改成常规的bool数组就过了,看来map和set的常数还是有点大。。像这种下标较大但下标范围较小的还是用常规的数组使下标加上一个常数比较好。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

int T;
ll a,b;
bool isprime[maxn];
vector<ll> prime;
bool isP[maxn];

void play_prime()
{
    memset(isprime,1,sizeof(isprime));
    isprime[1]=0;
    for(int i=2;i<maxn;i++){
        if(!isprime[i]) continue;
        for(int j=i*2;j<maxn;j+=i){
            isprime[j]=0;
        }
    }
    for(int i=1;i<maxn;i++){
        if(isprime[i]) prime.push_back(i);
    }
}

int main()
{
    cin>>T;
    int tag=1;
    play_prime();
    while(T--){
        scanf("%lld%lld",&a,&b);
        if(a<=1) a=2;
        memset(isP,1,sizeof(isP));
        for(int i=0;i<prime.size()&&prime[i]*prime[i]<=b;i++){
            ll p=prime[i];
            for(ll j=a+(p-a%p)%p;j<=b;j+=p){
                if(j!=p) isP[j-a]=0;
            }
        }
        int ans=0;
        for(ll i=a;i<=b;i++){
            if(isP[i-a]) ans++;
        }
        printf("Case %d: %d\n",tag++,ans);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/--560/p/4565797.html

变分模态分解(Variational Mode Decomposition, VMD)是一种强大的非线性、无参数信号处理技术,专门用于复杂非平稳信号的分析与分解。它由Eckart Dietz和Herbert Krim于2011年提出,主要针对传统傅立叶变换在处理非平稳信号时的不足。VMD的核心思想是将复杂信号分解为一系列模态函数(即固有模态函数,IMFs),每个IMF具有独特的频率成分和局部特性。这一过程与小波分析或经验模态分解(EMD)类似,但VMD通过变分优化框架显著提升了分解的稳定性和准确性。 在MATLAB环境中实现VMD,可以帮助我们更好地理解和应用这一技术。其核心算法主要包括以下步骤:首先进行初始化,设定模态数并为每个模态分配初始频率估计;接着采用交替最小二乘法,通过交替最小化残差平方和以及模态频率的离散时间傅立叶变换(DTFT)约束,更新每个模态函数和中心频率;最后通过迭代优化,在每次迭代中优化所有IMF的幅度和相位,直至满足停止条件(如达到预设迭代次数或残差平方和小于阈值)。 MATLAB中的VMD实现通常包括以下部分:数据预处理,如对原始信号进行归一化或去除直流偏置,以简化后续处理;定义VMD结构,设置模态数、迭代次数和约束参数等;VMD算法主体,包含初始化、交替最小二乘法和迭代优化过程;以及后处理,对分解结果进行评估和可视化,例如计算每个模态的频谱特性,绘制IMF的时频分布图。如果提供了一个包含VMD算法的压缩包文件,其中的“VMD”可能是MATLAB代码文件或完整的项目文件夹,可能包含主程序、函数库、示例数据和结果可视化脚本。通过运行这些代码,可以直观地看到VMD如何将复杂信号分解为独立模态,并理解每个模态的物理意义。 VMD在多个领域具有广泛的应用,包括信号处理(如声学、振动、生物医学信号分析)、图像处理(如图像去噪、特征提取)、金融时间序列分析(识
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值