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 ...
随机推荐
- Java 多线程通信之多生产者/多消费者
// 以生产和消费烤鸭为例 class Resource { private String name; private int count = 1; // 记录烤鸭的编号 private boolea ...
- java URL 利用网址api 查出手机号归属地
手机号码归属地查询api接口 1.淘宝网API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号码参数:tel:手机号码返 ...
- vc判断当前用户是否具有administrator 权限代码
BOOL IsAdmin() { HANDLE hAccessToken; BYTE * InfoBuffer = ]; PTOKEN_GROUPS ptgGroups; DWORD dwInfoBu ...
- If 条件控制 & while循环语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: i ...
- JSONObject和JSONArray 以及Mybatis传入Map类型参数
import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray JSONArray jsonArray = new ...
- 吴超老师课程---Hadoop的分布式集群安装
1.hadoop的分布式安装过程 1.1 分布结构 主节点(1个,是hadoop0):NameNode.JobTracker.SecondaryNameNode 从节点(2个,是 ...
- django-ORM复习补充
建表 class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() # ...
- jquery jsonp请求错误处理
jQuery将ajax方法进行了封装,jsonp可以轻松实现跨域请求. $.ajax({type: 'GET', url: "<%= SysUtils::TASK_CENTER_URL ...
- Java集合(9):ConcurrentHashMap
一.ConcurrentHashMap介绍 我们可以在单线程时使用HashMap提高效率,而多线程时用Hashtable来保证安全.但是,HashMap中未进行同步考虑,而Hashtable则使用了s ...
- 调试利器之tcpdump详解
简介你执行 man tcpdump 命令,你会看到文档中对tcpdump的说明是“dump traffic on a network”.可见,tcpdump是一个根据使用者的定义对网络上的数据包进行截 ...