zoukankan      html  css  js  c++  java
  • HDU

    HDU - 2824

    题意:

      求【a,b】间的欧拉函数和。这道题卡内存,只能开一个数组。

    思路:

      ϕ(n) = n * (p-1)/p * ... 可利用线性筛法求出所有ϕ(n) 。

      

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <iomanip>
    #include   <cstdlib>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include    <bitset>
    #include    <cctype>
    #include     <queue>
    #include     <cmath>
    #include      <list>
    #include       <map>
    #include       <set>
    using namespace std;
    //#pragma GCC optimize(3)
    //#pragma comment(linker, "/STACK:102400000,102400000")  //c++
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int ,pii> p3;
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define OKC ios::sync_with_stdio(false);cin.tie(0)
    #define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
    #define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
    //priority_queue<int ,vector<int>, greater<int> >que;
    
    const ll mos = 0x7FFFFFFFLL;  //2147483647
    const ll nmos = 0x80000000LL;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
    const double PI=acos(-1.0);
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    // #define _DEBUG;         //*//
    #ifdef _DEBUG
    freopen("input", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif
    /*-----------------------show time----------------------*/
                const int maxn = 3000009;
                
                
                ll sum[maxn];
                /*
                     const int maxn = 3000009;
                    int prime[316819];
                    int phi[maxn];
                    ll sum[maxn];
                    bool flag[maxn];
                    void getphi(){
                        phi[1] = 1,sum[1] = 1; 
                        int tot = 0;
                        for(int i=2; i<maxn; i++){
                            if(flag[i] == 0){
                                prime[++tot] = i;
                                phi[i] = i-1;
                            }
                            for(int j=1; i * prime[j] < maxn && j<=tot; j++){
                                flag[i*prime[j]] = 1;
                                if(i%prime[j] == 0){
                                    phi[i * prime[j]] = phi[i] * prime[j];
                                    break; 
                                }
                                else phi[i * prime[j]] = phi[i]*(prime[j] - 1);
                            }
                            sum[i] = sum[i-1] + phi[i];
                        }
                    
                     }
                */
                void getphi(){
                        sum[1] = 1;
                        for(int i=2; i<maxn; i++){
                            if(!sum[i]){
                                for(int j=i; j<maxn; j+=i){
                                    if(!sum[j])sum[j] = j;
                                    sum[j] = sum[j] / i * (i-1);
                                }
                            }
                        }    
                        for(int i=1; i<maxn; i++)sum[i] += sum[i-1];        
                }
    int main(){ 
                int a,b;
                getphi();
                while(~scanf("%d%d", &a, &b)){
                    printf("%lld
    ", sum[b] - sum[a-1]);
                }
    
                return 0;
    }
    HDU - 2824

    还有POJ的2478也是区间累计欧拉函数的和

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <iomanip>
    #include   <cstdlib>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include    <bitset>
    #include    <cctype>
    #include     <queue>
    #include     <cmath>
    #include      <list>
    #include       <map>
    #include       <set>
    using namespace std;
    //#pragma GCC optimize(3)
    //#pragma comment(linker, "/STACK:102400000,102400000")  //c++
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int ,pii> p3;
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define OKC ios::sync_with_stdio(false);cin.tie(0)
    #define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
    #define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
    //priority_queue<int ,vector<int>, greater<int> >que;
    
    const ll mos = 0x7FFFFFFFLL;  //2147483647
    const ll nmos = 0x80000000LL;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
    const double PI=acos(-1.0);
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    // #define _DEBUG;         //*//
    #ifdef _DEBUG
    freopen("input", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif
    /*-----------------------show time----------------------*/
                const int maxn = 1000009;
                
                
                ll sum[maxn];
                void getphi(){
                        sum[1] = 0;
                        for(int i=2; i<maxn; i++){
                            if(!sum[i]){
                                for(int j=i; j<maxn; j+=i){
                                    if(!sum[j])sum[j] = j;
                                    sum[j] = sum[j] / i * (i-1);
                                }
                            }
                        }    
                        for(int i=1; i<maxn; i++)sum[i] += sum[i-1];        
                }
    int main(){ 
                int a;
                getphi();
                while(~scanf("%d", &a) && a){
                    printf("%lld
    ", sum[a]);
                }
    
                return 0;
    }
    POJ2478

      

  • 相关阅读:
    1030 完美数列 (25 分)
    1029 旧键盘 (20 分)
    数据库命令失败原因汇总
    代码有中文括号,导致错误
    win10笔记本触控板使用指南
    (已解决)vsC#控制台应用添加System.Windows.Forms引用失败(精通C#)
    ildasm中Ctrl+M闪退的问题(已解决, 精通C# 15.1)
    C#控制台应用(.NET Core)添加System.Windows.Forms失败(已解决)
    知识点_指针_增加对指针的理解
    自己写出的Bug_应是%f却写成%d
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/9528018.html
Copyright © 2011-2022 走看看