zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    将PHP文件生成静态文件源码
    Entity Framework Code First 学习日记(6)一对多关系
    Entity Framework Code First 学习日记(5)
    Entity Framework Code First 学习日记(3)
    Entity Framework Code First 学习日记(7)多对多关系
    Entity Framework Code First学习日记(2)
    Entity Framework Code First 学习日记(8)一对一关系
    Entity Framework Code First 学习日记(9)映射继承关系
    Entity Framework Code First 学习日记(10)兼容遗留数据库
    Entity Framework Code First 学习日记(4)
  • 原文地址:https://www.cnblogs.com/pk28/p/5574137.html
Copyright © 2011-2022 走看看