light oj 1007 - Mathematically Hard

Posted 风子磊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了light oj 1007 - Mathematically Hard相关的知识,希望对你有一定的参考价值。

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\n", t++, sum[b]-sum[a-1]);
    }
    return 0;
}
View Code

 

 

 

以上是关于light oj 1007 - Mathematically Hard的主要内容,如果未能解决你的问题,请参考以下文章

(light OJ 1005) Rooks dp

Light oj 1422 - Halloween Costumes

(状压) Brush (IV) (Light OJ 1018)

light oj 1317

light oj1074

Light oj 1379 -- 最短路