码迷,mamicode.com
首页 > 其他好文 > 详细

light oj 1007 - Mathematically Hard

时间:2017-12-31 18:10:54      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:note   include   core   pac   cto   efi   task   src   函数   

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

标签:note   include   core   pac   cto   efi   task   src   函数   

原文地址:https://www.cnblogs.com/zhulei2/p/8158026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!