• Lightoj 1007


    1007 - Mathematically Hard
    Time Limit: 2 second(s) Memory Limit: 64 MB

    Mathematically some problems look hard. But with the help of the computer, some problems can be easily solvable.

    In this problem, you will be given two integers a and b. You have to find the summation of the scores of the numbers from a to b (inclusive). The score of a number is defined as the following function.

    score (x) = n2, where n is the number of relatively prime numbers with x, which are smaller than x

    For example,

    For 6, the relatively prime numbers with 6 are 1 and 5. So, score (6) = 22 = 4.

    For 8, the relatively prime numbers with 8 are 1, 3, 5 and 7. So, score (8) = 42 = 16.

    Now you have to solve this task.

    Input

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

    Each case will contain two integers a and b (2 ≤ a ≤ b ≤ 5 * 106).

    Output

    For each case, print the case number and the summation of all the scores from a to b.

    Sample Input

    Output for Sample Input

    3

    6 6

    8 8

    2 20

    Case 1: 4

    Case 2: 16

    Case 3: 1237

    Note

    Euler's totient function  applied to a positive integer n is defined to be the number of positive integers less than or equal to n that are relatively prime to n.  is read "phi of n."

    Given the general prime factorization of , one can compute  using the formula

    欧拉函数打表。抄的大白书上的板子。一开始没看note  自己yy筛法。果然很弱智,题目已经给了Note了,眼瞎。

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/6/10 19:35:28
    File Name     :1007.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 5000100
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int phi[maxn];
    ull sum[maxn];
    void phi_table(int n){
        for(int i=2;i<=n;i++)phi[i]=0;
        phi[1]=1;
        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);
            }
        }
        sum[1]=0;
        for(int i=2;i<=maxn;i++){
            sum[i]=sum[i-1]+(ull)phi[i]*phi[i];
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        phi_table(maxn);
        int T,a,b;
        cin>>T;
        for(int t=1;t<=T;t++){
            scanf("%d%d",&a,&b);
            printf("Case %d: %llu
    ",t,sum[b]-sum[a-1]);
        }
        return 0;
    }

     

  • 相关阅读:
    野生的男人,家养的猪
    能在xcode5中开发基于IOS7sdk的应用程序兼容ios4.3之后的系统吗?
    ios开发怎样才能做到代码和界面彻底分离,方便换肤?
    如何解决iOS6、iOS7 3.5寸和4.0寸屏的适配问题?不要写两个xib文件
    哪些听起来很牛逼的互联网理念!
    iOS 使用宏 常量 报错 expected expression
    ios测试宏指令出错:“Expected identefier”
    某个 页面覆盖了 UITabBar 的tabItem的解决办法
    ios(包括6、7)应用程序引用系统通讯录的方法 [亲测可行]
    ios 获得通讯录中联系人的所有属性 亲测,可行 兼容io6 和 ios 7
  • 原文地址:https://www.cnblogs.com/pk28/p/5574137.html
Copyright © 2020-2023  润新知