hdu 2824 线性筛素数求欧拉函数+前缀和

来源:互联网 发布:如何开展网络推广工作 编辑:程序博客网 时间:2024/06/02 05:16

因为只是区间查询,没有修改,所以只需要提前求出前缀和即可,本题难点在于数据量大导致数组用起来比较紧张,所以要重复使用顶用过的数组

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX 3000001using namespace std;int p[1000000],pt;long long phi[MAX];bool  m[MAX];int N = 3000001;void init (){    memset ( m , 0 , sizeof ( m ) );    pt = 0;    phi[1] = 1;    int k;    for ( int i = 2 ; i < N ; i++ )    {        if (!m[i]) p[pt++]=i,phi[i]=i-1,m[i]=1;        for ( int j=0;j<pt&&(k=i*p[j])<N;j++)        {            m[k] = 1;            if ( i%p[j]==0 )            {                phi[k]=phi[i]*p[j];            }            else phi[k]=phi[i]*(p[j]-1);        }    } } int main ( ){    init ();    for ( int i = 2 ; i < N ; i++ )        phi[i] = phi[i]+phi[i-1];    int left , right;    while ( ~scanf ( "%d%d" , &left , &right ) )        printf ( "%I64d\n" , phi[right]-phi[left-1] );    return 0;}


0 0
原创粉丝点击