lightoj1370欧拉函数/素数筛
这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; ll Euler[N];
void euler()
{
Euler[]=;
for(ll i=;i<N;i++)Euler[i]=i;
for(ll i=;i<N;i++)
if(Euler[i]==i)
for(ll j=i;j<N;j+=i)
Euler[j]-=Euler[j]/i;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
ll t,n,cnt=;
euler();
cin>>t;
while(t--){
cin>>n;
ll ans=;
for(ll i=;i<n;i++)
{
ll a;
cin>>a;
for(ll j=a+;j<N;j++)
if(Euler[j]==j-)
{
ans+=j;
break;
}
}
cout<<"Case "<<++cnt<<": "<<ans<<" Xukha"<<endl;
}
return ;
}
/*********************
3
5
1 2 3 4 5
6
10 11 12 13 14 15
2
1 1
*********************/
euler
2是直接用素数筛
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; bool prime[N];
void getprime()
{
memset(prime,,sizeof prime);
prime[]=;
for(int i=;i<N;i++)
{
if(!prime[i])
{
for(int j=*i;j<N;j+=i)
prime[j]=;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
ll t,n,cnt=;
getprime();
cin>>t;
while(t--){
cin>>n;
ll ans=;
for(ll i=;i<n;i++)
{
ll a;
cin>>a;
for(ll j=a+;j<N;j++)
if(!prime[j])
{
ans+=j;
break;
}
}
cout<<"Case "<<++cnt<<": "<<ans<<" Xukha"<<endl;
}
return ;
}
/*********************
3
5
1 2 3 4 5
6
10 11 12 13 14 15
2
1 1
*********************/
prime
后者仅花了56ms,前者120ms
lightoj1370欧拉函数/素数筛的更多相关文章
- 【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛
题目描述 求 输入 第一行包含一个正整数T,表示有T组测试数据.接下来T<=10^5行,每行给出一个正整数N,N<=10^6. 输出 包含T行,依次给出对应的答案. 样例输入 7 1 10 ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
- Bi-shoe and Phi-shoe(欧拉函数/素筛)题解
Bi-shoe and Phi-shoe Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe ...
- Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3241 Solved: 1437[Submit][Status][Discuss ...
- lightOJ1370 欧拉函数性质
D - (例题)欧拉函数性质 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB ...
- 【bzoj2190】【仪仗队】欧拉函数+线性筛(浅尝ACM-J)
向大(hei)佬(e)势力学(di)习(tou) Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪 ...
- Bi-shoe and Phi-shoe 欧拉函数 素数
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...
- BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)
一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...
随机推荐
- Type Java compiler level does not match the version of the installed Java project facet.项目内容没错但是项目上报错,不影响运行
1.Window->Show View->Problems 2.在项目上右键properties->project Facets->修改右侧的version 保持一致 3.w ...
- celery中的生产者消费者问题
celery中的生产者消费者问题 在task1.py文件中: # demo1:task.py and celery.py in one file# run it byfrom celery impor ...
- web 开发常见问题--Session 与 Cookie 却别
总结: 1.首先,session与cookie都是保存数据的,存在的原因很大程度上是为了解决HTTP协议的无状态特性 2.都是保存数据,却别在于cookie保存在客户端,由浏览器管理,session保 ...
- 004-安装CentOS7后需要的操作
1 安装EPEL源 EPEL即Extra Packages for Enterprise Linux,是基于Fedora的一个项目,为红帽系的操作系统提供额外的软件包,适用于RHEL.CentOS和S ...
- Java基础—数组(转载)
Java 语言中提供的数组是用来存储固定大小的同类型元素.其实数组就是一个容器. 创建数组 Java 中声明数组的语法有两种: dataType[] arrayRefVar; // 首选的方法 dat ...
- 解决127.0.0.1 localhost 劫持问题
在一个安装iis的过程中,把网站部署上去之后就发现127.0.0.1或者localhost都会跳转到一个莫名的网站,发现断网之后就是会跳转到一个Http://www.76636.com 类似这种的网站 ...
- 简明python教程二-----对象
物理行:是你在编写程序时所看见的. 逻辑行:是Python看见的单个语句. 默认的,python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一个物理行中使用多于一个逻辑行,用分号“: ...
- python16_day13【css、js】
一.部局 1.pg-header(title) .pg-header{ height: 48px; background-color: cadetblue; line-height: 48px; mi ...
- rails 单数 复数 大写 小写转换 下划线 驼峰命名
downcase 变小写 pluralize 复数 singularize 单数 camelcase 驼峰 underscore : “MyScore”.undersocre ==> my_s ...
- js防抖
那我们设置个 options 作为第三个参数,然后根据传的值判断到底哪种效果,我们约定: leading:false 表示禁用第一次执行trailing: false 表示禁用停止触发的回调 我们来改 ...