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. C#点击按钮添加标签

    <asp:Button ID="button1" runat="server" Text="创建" onclick="But ...

  2. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  3. linux之wc命令详解

    Linux系统中wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式 wc [options] 文件... 2.命令功能 统计指定文件中的字 ...

  4. Linux系统时钟与硬件时钟

    linux系统有两个时钟:一个是由主板电池驱动的硬件时钟(Real Time Clock),也叫做RTC或者叫CMOS时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行的系统是不用这个时间的: ...

  5. linux 配置本地yum

    1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...

  6. hash 模式与 history 模式小记

    hash 模式 这里的 hash 就是指 url 后的 # 号以及后面的字符.比如说 "www.baidu.com/#hashhash" ,其中 "#hashhash&q ...

  7. Turbine使用

    一.简介 Turbine是聚合服务器发送事件流数据的一个工具,Hystrix的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过Turbine来监控集群下Hystrix的metrics情况 T ...

  8. mybatis处理集合、数组参数使用in查询等语句的两种方法

    对于mybatis的参数类型是集合数组的时候进行查询. 第一种:参数list使用mybatis的标签 SELECT * FROM TABLE_NAME AS a <where> <i ...

  9. Spring boot 数据源配置。

    配置文件 : spring boot  配置文件 有两种形式 ,一种是properties文件.一种是yml文件.案列使用properties文件. 数据源的默认配置 : spring boot 约定 ...

  10. new Date()与setDate()参数

    New Date()与setDate()参数 相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结: new Date() new Date() 一共有六种形式,五种带参数 ...