Lightoj 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
欧拉函数打表。抄的大白书上的板子。一开始没看note 自己yy筛法。果然很弱智,题目已经给了Note了,眼瞎。
/* ***********************************************
Author :guanjun
Created Time :2016/6/10 19:35:28
File Name :1007.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 5000100
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int phi[maxn];
ull sum[maxn];
void phi_table(int n){
for(int i=;i<=n;i++)phi[i]=;
phi[]=;
for(int i=;i<=n;i++)if(!phi[i]){
for(int j=i;j<=n;j+=i){
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
sum[]=;
for(int i=;i<=maxn;i++){
sum[i]=sum[i-]+(ull)phi[i]*phi[i];
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
phi_table(maxn);
int T,a,b;
cin>>T;
for(int t=;t<=T;t++){
scanf("%d%d",&a,&b);
printf("Case %d: %llu\n",t,sum[b]-sum[a-]);
}
return ;
}
Lightoj 1007 - Mathematically Hard的更多相关文章
- lightoj 1007 - Mathematically Hard 欧拉函数应用
题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...
- 1007 - Mathematically Hard
1007 - Mathematically Hard PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 6 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- lightoj 1007
预先处理好phi数组和前缀和,水题. #include<cstdio> #include<string> #include<cstring> #include< ...
- LightOj1007 - Mathematically Hard(欧拉函数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1007 题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ...
- SCNU 2015ACM新生赛初赛【1007. ZLM的扑克牌】解题报告
题目链接详见SCNU 2015新生网络赛 1007. ZLM的扑克牌 . 其实我在想这题的时候,还想过要不要设置求最小的排列,并且对于回文数字的话,可以把扑克牌折起来( ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- PKU 1007
题名:DNA排序 题意:给定字符串长度.个数,计算每个字符串的逆序数,然后从大到小排列,有兴趣的可以去看下原题. 计算字符串逆序数,然后排序,这里使用了快速排序算法,string释放的时候竟然有问题, ...
随机推荐
- 【HDU 6153】A Secret (KMP)
Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...
- Web安全解决方案
什么是 .NET Framework 安全性? .NET Framework 提供了用户和代码安全模型,允许对用户和代码可以执行的操作进行限制.要对基于角色的安全性和代码访问安全性进行编程,可以从 S ...
- Python MySQLdb 查询中文出现问号的解决方法
在连接数据库的时候设置如下即可: db = MySQLdb.connect('localhost','root','×××××','test',use_unicode=True,charset=&qu ...
- SQL ROW_NUMBER() 通用分页存储过程
--提取分页数据,返回总记录数 Create procedure [dbo].[sp_Common_GetDataPaging_ReturnDataCount] ( @SqlString varcha ...
- [luoguP1169] [ZJOI2007]棋盘制作(单调栈)
传送门 和玉蟾宫差不多 ——代码 #include <cstdio> #include <iostream> using namespace std; ; int n, m, ...
- [luoguP1027] Car的旅行路线(Floyd)
传送门 建图麻烦,建完图搞一遍Floyd就好了. ——代码 #include <iostream> #include <cstdio> #include <cmath&g ...
- 2016 Multi-University Training Contest 9 solutions BY 金策工业综合大学
A Poor King Tag: Reversed BFS Preprocessing is needed to calculate answers for all positions (states ...
- 【BZOJ1031】字符加密Cipher(后缀数组)
题意:将一个长度为2n(复制粘贴后)的字符串的所有长度为n的后缀从小到大排序,并依次输出它们的最后一个字母. n<=100000 思路:裸SA,模板真难背 P党不得不写成C++风格 ..]of ...
- 创建SSH keys
1.检查是否已经有SSH Key存在 windows: type "%userprofile%\.ssh\id_rsa.pub" Linux: cat ~/.ssh/id_rsa. ...
- BZOJ 2308 莫队入门经典
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2038 参考博客 https://www.cnblogs.com/Paul-Guderi ...