【BZOJ3944】Sum
题面
Description
Input
一共T+1行
第1行为数据组数T(T<=10)
第2~T+1行每行一个非负整数N,代表一组询问
Output
一共T行,每行两个用空格分隔的数ans1,ans2
Sample Input
6
1
2
8
13
30
2333
Sample Output
1 1
2 0
22 -2
58 -3
278 -3
1655470 2
题目分析
杜教筛模板题。(垃圾卡常题)
套套路即可。
代码实现
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<map>
#define MAXN 0x7fffffff
typedef long long LL;
const int N=3e6+5,M=3e6;
using namespace std;
inline int Getint(){register int x=0,f=1;register char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}return x*f;}
int prime[N];
bool vis[N];
int mu[N];
map<int,int>smu;
int Smu(int x){
if(x<=M)return mu[x];
if(smu[x])return smu[x];
int ret=1;
for(int l=2,r=0;r!=x;l=r+1){
r=x/(x/l);
ret-=1ll*(r-l+1)*Smu(x/l);
}
return smu[x]=ret;
}
LL phi[N];
map<int,LL>sphi;
LL Sphi(int x){
if(x<=M)return phi[x];
if(sphi[x])return sphi[x];
LL ret=1ll*x*(1ll*x+1)/2;
for(int l=2,r=0;r!=x;l=r+1){
r=x/(x/l);
ret-=1ll*(r-l+1)*Sphi(x/l);
}
return sphi[x]=ret;
}
int main(){
phi[1]=mu[1]=1;
for(int i=2;i<=M;i++){
if(!vis[i])prime[++prime[0]]=i,mu[i]=-1,phi[i]=i-1;
for(int j=1;j<=prime[0]&&i*prime[j]<=M;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*phi[prime[j]];
mu[i*prime[j]]=-mu[i];
}
}
for(int i=2;i<=M;i++)phi[i]+=phi[i-1],mu[i]+=mu[i-1];
int T=Getint();
while(T--){
int n=Getint();
cout<<Sphi(n)<<' '<<Smu(n)<<'\n';
}
return 0;
}
【BZOJ3944】Sum的更多相关文章
- 【BZOJ3944】Sum(杜教筛)
[BZOJ3944]Sum(杜教筛) 题面 求\[\sum_{i=1}^n\mu(i)和\sum_{i=1}^n\phi(i)\] 范围:\(n<2^{31}\) 令\[S(n)=\sum_{i ...
- 【BZOJ3944】 Sum
Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1,ans ...
- 【CF914G】Sum the Fibonacci 快速??变换模板
[CF914G]Sum the Fibonacci 题解:给你一个长度为n的数组s.定义五元组(a,b,c,d,e)是合法的当且仅当: 1. $1\le a,b,c,d,e\le n$2. $(s_a ...
- 【BZOJ4262】Sum 单调栈+线段树
[BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- 【loj6059】Sum
Portal --> loj6059 Solution 看过去第一反应是..大力数位dp!然后看了一眼数据范围... 但是这没有什么关系!注意到我们不需要考虑前导零了,可以直接快乐dp ...
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...
随机推荐
- 1、linux常用命令的英文单词缩写
1.linux常用命令的英文单词缩写 命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户 rpm:redhat pa ...
- UVA-10200-Prime Time-判断素数个数(打表预处理)+精度控制
题意: 给出a.b区间,判断区间内素数所占百分比 思路: 注意提前打表和控制精度1e-8的范围足够用了 细节: 精度的处理 判断素数的方法(且返回值为bool) 数据类型的强制转换 保存素数个数 提前 ...
- 2019 USP Try-outs 练习赛
// 好久没更博客了,最近打了很多场练习赛&校内PK赛,大概自闭忙于补题吧 // 9.26 周四练习赛 A. Kolkhozy 题意 有 n 个数 \(f[i]\) ,有 q 次询问(l, r ...
- 面试问烂的 MySQL 查询优化,看完屌打面试官!
Java技术栈 ,一般把连接数设置得大一些). 并发量:同一时刻数据库服务器处理的请求数量 3.超高的 CPU使用率:CPU资源耗尽出现宕机. 4.磁盘 IO:磁盘 IO性能突然下降.大量消耗磁盘性能 ...
- Spring源码分析(一):从哪里开始看spring源码(系列文章基于Spring5.0)
概述 对于大多数第一次看spring源码的人来说,都会感觉不知从哪开始看起,因为spring项目源码由多个子项目组成,如spring-beans,spring-context,spring-core, ...
- Unity中DoTween的使用
在Unity手游开发中,经常用到插值运算,我们可以使用Mathf.Lerp自行去实现效果,但是使用插件提高了我们的开发效率,这里归结一下DoTween的基本使用方式以及效果说明: 直接代码: usin ...
- Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解
基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...
- yum -y install python-devel
yum -y install python-devel的时候报错如图: Could not retrieve mirrorlist http://mirrorlist.centos.org/?rele ...
- 【JZOJ6274】梦境
description analysis 其实可以贪心 先把区间按左端点排序,转折点也排序 扫一次转折点,把所有左端点在当前点左边的区间丢进优先队列里 按照贪心策略,对于某个转折点,一定选择右端点离它 ...
- 【JZOJ3422】水叮当的舞步
description 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变. 为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ 地毯上 ...