• LightOj1007欧拉函数


    链接:http://lightoj.com/volume_showproblem.php?problem=1007

    题意:给一段闭区间,求区间中每一个数的欧拉函数值的平方和。

    思路:思路很简单,先打表,求出1到5e6的欧拉函数值,然后再求平方和。

    思路虽然简单,但是这道题还是A得好艰难。T,WA,超内存都出现了,泪。一个要注意的是由于数据过大,要用unsigned long long,不然就WA了。还有就是求平方和的时候,如果每次都用一个循环来求的话,会T掉,所以预先求出前 i 项的平方和。在求平方和的过程中,如果再开一个数组来保存前 i 项和的话,会超内存。有点奇怪的是,a,b都是小于等于5e6,在求每个数的欧拉函数值的时候,最大只要求到5e6就够了,但是取n=5e6会WA,不应该啊。。。满满都是泪。

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    typedef unsigned long long LL;
    const int maxn=5000005;
    LL phi[maxn];
    //LL count[maxn];
    int n=5e6+5;
    void phi_table()
    {
        memset(phi,0,sizeof(phi));
        for(int i=2;i<=n;i++)
            if(!phi[i])
               for(int j=i;j<=n;j+=i)
               {
                  if(!phi[j])
                     phi[j]=j;
                  phi[j]=phi[j]/i*(i-1);
               }
    //     count[1]=0;//超内存
    //     for(int i=2;i<n;i++)
    //         count[i]=count[i-1]+phi[i]*phi[i];
        for(int i=2;i<n;i++)
            phi[i]=phi[i-1]+phi[i]*phi[i];
    }
    int main()
    {
        int t,a,b,ca=1;
        LL ans;
    
        phi_table();
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&a,&b);
    //        ans=0;//T!
    //        for(int i=a;i<=b;i++)
    //            ans+=phi[i]*phi[i]
            ans=phi[b]-phi[a-1];
            printf("Case %d: %llu
    ",ca++,ans);
        }
        return 0;
    }


     

    究竟是我抛弃了历史,还是历史遗弃了我。
  • 相关阅读:
    c/c++指针
    C++小思
    gvim-ide plugins
    Windows下文件的所有和权限
    C++枚举类型
    linux的cgroup控制
    linux的free命令
    linux下可以禁用的一些服务
    bat programming is easy and powerful
    c++类定义代码的分离
  • 原文地址:https://www.cnblogs.com/54zyq/p/3199607.html
Copyright © 2020-2023  润新知