zoukankan      html  css  js  c++  java
  • light oj 1007

    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

    题目大意:求出a到b之间所有数本身与小于其本身互素的数的个数的平方和(欧拉函数打表,区间平方和打表)。

    题不是很难卡在了数据类型上,错了好几次(反思)。需要用到unsigned long long

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #define LL unsigned long long
    using namespace std;
    int phi[5000005]={0};
    LL sum[5000005]={0};
    void inin1()
    {
        for(int i=2; i<5000002; i++)
        {
            if(phi[i]==0)
            for(int j=i; j<=5000002; j+=i)
            {
                if(phi[j]==0)phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
    void inin2()
    {
        for(int i=2; i<=5000000; i++)
            sum[i]=sum[i-1]+(LL)phi[i]*(LL)phi[i];
    }
    int main()
    {
        inin1();
        inin2();
        int T, t=1;
        scanf("%d", &T);
        while(T--)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            printf("Case %d: %llu
    ", t++, sum[b]-sum[a-1]);
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    trackr: An AngularJS app with a Java 8 backend – Part III
    trackr: An AngularJS app with a Java 8 backend – Part II
    21. Wireless tools (无线工具 5个)
    20. Web proxies (网页代理 4个)
    19. Rootkit detectors (隐形工具包检测器 5个)
    18. Fuzzers (模糊测试器 4个)
    16. Antimalware (反病毒 3个)
    17. Debuggers (调试器 5个)
    15. Password auditing (密码审核 12个)
    14. Encryption tools (加密工具 8个)
  • 原文地址:https://www.cnblogs.com/zhulei2/p/8158026.html
Copyright © 2011-2022 走看看