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

思路:就是求欧拉函数,先素数打表,再欧拉函数打表,最后求下前缀和;

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 using namespace std;
8 bool pr[5*1000006]={0};
9 int prime[5*100006];
10 unsigned long long oula[5*1000006];
11 typedef long long LL;
12 int main(void)
13 {
14 LL i,j,p,q;
15 int s,k;
16 scanf("%d",&k);pr[0]=true;
17 pr[1]=true;
18 for(i=0;i<5*1000006;i++)
19 oula[i]=i;
20 for(i=2;i<10000;i++)
21 {
22 if(!pr[i])
23 {
24 for(j=i;i*j<=5*1000000;j++)
25 {
26 pr[i*j]=true;
27 }
28 }
29 }int cnt=0;
30 for(i=2;i<5*1000005;i++)
31 {
32 if(pr[i]==false)
33 prime[cnt++]=i;
34 }
35 for(i=0;i<cnt;i++)
36 {
37 for(j=1;prime[i]*j<=5*1000000;j++)
38 {
39 oula[prime[i]*j]=oula[prime[i]*j]/prime[i]*(prime[i]-1);
40 }
41 }
42 for(i=2;i<=1000000*5;i++)
43 {
44 oula[i]=oula[i-1]+oula[i]*oula[i];
45 }
46 for(s=1;s<=k;s++)
47 {
48 scanf("%lld %lld",&p,&q);printf("Case %d: ",s);
49 printf("%llu\n",oula[q]-oula[p-1]);
50 }
51 return 0;
52 }

1007 - Mathematically Hard的更多相关文章

  1. Lightoj 1007 - Mathematically Hard

    1007 - Mathematically Hard    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 6 ...

  2. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  3. lightoj 1007 - Mathematically Hard 欧拉函数应用

    题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...

  4. SCNU 2015ACM新生赛初赛【1007. ZLM的扑克牌】解题报告

            题目链接详见SCNU 2015新生网络赛 1007. ZLM的扑克牌 .         其实我在想这题的时候,还想过要不要设置求最小的排列,并且对于回文数字的话,可以把扑克牌折起来( ...

  5. PKU 1007

    题名:DNA排序 题意:给定字符串长度.个数,计算每个字符串的逆序数,然后从大到小排列,有兴趣的可以去看下原题. 计算字符串逆序数,然后排序,这里使用了快速排序算法,string释放的时候竟然有问题, ...

  6. sicily 1007. To and Fro 2016 11 02

    // Problem#: 1007// Submission#: 4893204// The source code is licensed under Creative Commons Attrib ...

  7. 【HDU 1007】Quoit Design

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 最近欧式距离模板题. 用分治大法(分治的函数名用cdq纯属个人习惯_(:з」∠)_) 一开始狂M. 后来判 ...

  8. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  9. poj 1007 DNA Sorting 解题报告

    题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...

随机推荐

  1. 微信小程序扫描普通二维码打开小程序的方法

    很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...

  2. 解决UE4项目编辑器启动时出现LogLinker: Warning: Failed to load '/Game/FirstPersonBP/FirstPersonOverview': Can't find file.

    UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 LogLinker: Warning: Failed to load '/Game/FirstPersonBP/Firs ...

  3. GraphScope 集群部署

    GraphScope 集群部署 1 k8s集群搭建 大致步骤如下: 安装docker.在ubuntu上,可以简单的通过命令sudo apt install docker.io来安装. 安装kubele ...

  4. accustom

    近/反义词: acclimatize, familiarize, habituate, inure, get used to, orient; alienate, estrange, wean New ...

  5. 【Reverse】每日必逆0x03

    BUU-刮开有奖 附件:https://files.buuoj.cn/files/abe6e2152471e1e1cbd9e5c0cae95d29/8f80610b-8701-4c7f-ad60-63 ...

  6. pyqt5 的串口编写进度

    2020.12.18 今天遇到一个问题, 想用回车实现串口数据的发送. 下面这句话是让光标移动到文字的尾部,但是不能够实现. 对QTextEdit控件中的文字改写,或清除后,再调用下面的移动到尾部,就 ...

  7. webservice--cxf和spring结合

    服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...

  8. 解决ViewPager与ScrollView 冲突

    ViewPager来实现左右滑动切换tab,如果tab的某一项中嵌入了水平可滑动的View就会让你有些不爽,比如想滑动tab项中的可水平滑动的控件,却导致tab切换. 因为Android事件机制是从父 ...

  9. maven的lifecycle

    1.maven clean. 清理项目的target目录 2.maven compile 编译项目 3.maven test 编译项目后,再执行Junit测试方法 4.maven package 编译 ...

  10. class.getName()和class.getSimpleName()的区别

    根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...