Lightoj 1370 素数打表 +二分
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of Bamboos of all possible integer lengths (yes!) are available in the market. According to Xzhila tradition,
Score of a bamboo = Φ (bamboo's length)
(Xzhilans are really fond of number theory). For your information, Φ (n) = numbers less than n which are relatively prime (having no common divisor other than 1) to n. So, score of a bamboo of length 9 is 6 as 1, 2, 4, 5, 7, 8 are relatively prime to 9.
The assistant Bi-shoe has to buy one bamboo for each student. As a twist, each pole-vault student of Phi-shoe has a lucky number. Bi-shoe wants to buy bamboos such that each of them gets a bamboo with a score greater than or equal to his/her lucky number. Bi-shoe wants to minimize the total amount of money spent for buying the bamboos. One unit of bamboo costs 1 Xukha. Help him.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 10000) denoting the number of students of Phi-shoe. The next line contains n space separated integers denoting the lucky numbers for the students. Each lucky number will lie in the range [1, 106].
Output
For each case, print the case number and the minimum possible money spent for buying the bamboos. See the samples for details.
Sample Input |
Output for Sample Input |
3 5 1 2 3 4 5 6 10 11 12 13 14 15 2 1 1 |
Case 1: 22 Xukha Case 2: 88 Xukha Case 3: 4 Xukha |
题意:给你一个数组a,找到一个最小的值x,使得phi(x)>=phi(a[i]);求x的和最小
思路:根据欧拉函数,一个素数p的欧拉函数值为p-1;所以最小的数为大于这个数的最小素数;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
const int N=1e3+,M=1e6+,inf=1e9+,mod=;
const int MAXN=;
int prime[MAXN];//保存素数
bool vis[MAXN];//初始化
int Prime(int n)
{
int cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
if(!vis[i])
prime[cnt++]=i;
for(int j=;j<cnt&&i*prime[j]<n;j++)
{
vis[i*prime[j]]=;
if(i%prime[j]==)
break;
}
}
return cnt;
}
int main()
{
int cnt=Prime(MAXN);
int x,y,z,i,t;
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&x);
ll ans=;
for(i=;i<x;i++)
{
scanf("%d",&y);
int st=;
int en=cnt-;
while(st<en)
{
int mid=(st+en)>>;
if(prime[mid]<=y)
st=mid+;
else
en=mid;
}
ans+=prime[st];
}
printf("Case %d: %lld Xukha\n",cas++,ans);
}
return ;
}
Lightoj 1370 素数打表 +二分的更多相关文章
- LightOJ 1259 Goldbach`s Conjecture 素数打表
题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...
- Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)
题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...
- LightOJ 1370 - Bi-shoe and Phi-shoe (欧拉函数思想)
http://lightoj.com/volume_showproblem.php?problem=1370 Bi-shoe and Phi-shoe Time Limit:2000MS Me ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- Codeforces Round #315 (Div. 2C) 568A Primes or Palindromes? 素数打表+暴力
题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以 ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
- HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛
题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #inc ...
- HDU 1397 Goldbach's Conjecture【素数打表】
题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...
随机推荐
- 《从零开始学Swift》学习笔记(Day 29)——访问级别
Swift 2.0学习笔记(Day 29)——访问级别 原创文章,欢迎转载.转载请注明:关东升的博客 访问级别: Swift提供了3种不同访问级别,对应的访问修饰符为:public.internal和 ...
- 关于微信小程序的开发步骤
~~~~包子最近在研究小程序 首先先讲一下小程序一些基本的步骤: 1.登录微信的公众平台,选择小程序,注册一个账号,期间有碰到什么交300块钱的就不要理他,因为我只是做demo,当然,准备上线的企业啥 ...
- UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按
#import "RootViewController.h" @interface RootViewController (){ UIImageView *imageView ...
- 0x03 MySQl 库操作
一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...
- T-SQL利用笛卡尔积累计、累加
T-SQL利用笛卡尔积累计.累加 笛卡尔积 --原始数据 select templateid,needitem1Count from db_tank..TS_CardMain --累计数据 selec ...
- 高阶函数,map,filter,sorted
Map:对列表中的每个元素进行操作 >>> def f(x): ... return x * x ... >>> map(f, [1, 2, 3, 4, 5, ...
- Android 结束进程的方法forceStopPackage
ActivityManager sd = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); Method method = Clas ...
- LeetCode:二进制求和【67】
LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...
- VMWare中安装windowsXP遇到的问题
XP系统安装 1.安装Windows和安装linux不一样,创建虚拟机完成后Linux自动根据硬盘进行系统安装,不需要提前分区.而windows必须进行提前分区,这个分区是在虚拟磁盘上完成的,就是你创 ...
- fastReport 绑定DataBand数据源后还是打印出一条数据
升级了fastreport到v2018后,打印出现问题,datasource是多条数据,可打印出来始终只显示第一条 DataBand dataBand = report.FindObject(&quo ...