code forces 1176 D. Recover it!
原题链接:https://codeforces.com/contest/1176/problem/D
本文链接:https://www.cnblogs.com/blowhail/p/11146761.html
题目大意是 两个个数列 a , b 相同 ,如果 ai 是素数,那么b数列里添加上第ai个素数(2为第一个),如果不是素数,那么b数列里添加上ai的最大因子。现在给出添加完之后的b数列,求出a数列。
大致思路: 先从大到小排序,然后判断是不是素数,如果是素数,那就遍历一遍,找到它是第几个素数,然后输出并记录下来。 如果不是素数,就直接输出,再求出最大的因子记录下来。
写超时了好多次,修改了很多地方,可读性估计不太好
代码如下
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define ll long long
#define pi 3.1415926
using namespace std;
int primes[],a[],b[],p2[];
//这里primes数组按顺序记录素数,p2是 素数为1 非素数为0
//a数组是存下输入的数列,b数组是记录每个数的个数
bool cmp (int x,int y)
{
return x>y;
}
void prime (int n)
{
int i,t, k,j;
primes[]=;
p2[]=;
int f,p=;
for (i=;i<=n;++i){
k=sqrt(i);
f=;
for (t=;t<=k;++t)
if(i%t==)
{
f=;
break;
}
if (f){
primes[p++]=i; //按顺序存
p2[i]=; //
}
}
}
int main ()
{
int i,t,m,n,l,k,j,p;
scanf("%d",&n);
j=*n;
for(i=;i<j;++i){
scanf("%d",&a[i]);
b[a[i]]++; //记录每个数个数
}
sort(a,a+j,cmp);
prime(a[]);
int c=; //这里我用c来记录每次遍历素数的位置,然后下一次就直接从这个位置继续遍历(因为已经从大到小排序了)
for (i=;i<j;++i)
{
if (b[a[i]]==)
continue;
if (p2[a[i]]==){
for (int h=c;h>=;--h)
if (primes[h]==a[i]){
c=h;
break;
}
printf("%d ",c);
b[ a[i] ]--;
b[c]--; //b是记录每个数出现次数,每次遍历完都把找出来的数给减去
}
else
{
printf("%d ",a[i]);
for (int h=;h<a[i];++h)
if (a[i]%h==)
{
k=a[i]/h;
break;
}
b[a[i]]--;
b[k]--;
}
}
printf("\n");
return ;
}
code forces 1176 D. Recover it!的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
- code forces Jeff and Periods
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- OpenGL(二十) glutSpecialFunc响应键盘方向控制键
OpenGL的glut中使用glutMouseFunc函数注册鼠标响应事件,使用glutKeyboardFunc函数注册键盘响应事件,对键盘上特殊的4个方向按键的响应函数是glutSpecialFun ...
- MSRA专访摘要
前段时间有幸参加微软亚洲研究院之旅,顺便投简历,没想到在两次访谈迎来,并且是连续 的两次被拒绝.严重的刺激到了我.导致我疯狂的复习刷Offer.如今最终算是告于段落.如今也最终有空沉下心来总结 总结近 ...
- MySQL 执行原生sql
public class MySqlHelper { private YourContext _context; public MySqlHelper(YourContext context) { _ ...
- 最简单的IdentityServer实现——项目基本结构与流程
项目结构 共分为三个组成部分: IdentityServer:用于登录.身份认证与授权 Api:提供获得授权后调用的各接口 Client(客户端,控制台):访问IdentityServer授权,再访问 ...
- WPF中ItemsControl应用虚拟化时找到子元素的方法
原文:WPF中ItemsControl应用虚拟化时找到子元素的方法 wpf的虚拟化技术会使UI的控件只初始化看的到的子元素, 而不是所有子元素都被初始化,这样会提高UI性能. 但是我们经常会遇到一个 ...
- 【WPF】SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考
原文:[WPF]SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考 MSDN上解释了一大堆,二者对比来看,并不能发现什么明显的区别,微软爸爸也不知道多 ...
- vxworks下libpcap的移植
linux下的libpcap应用能够成熟的使用在第三方的应用中,但基于vxworks开发的项目中需要使用libpcap的部分功能则无相应的实现. 研究了下libpcap向vxworks的移植,并且小有 ...
- golang生成c-shared so供c语言或者golang调用到例子
1.golang生成c-shared类型到so 建立文件夹hello,创建main.go文件,内容如下 package main import "C" func main() {} ...
- 零元学Expression Blend 4 - Chapter 3 熟悉操作第一步(制作一个猴子脸)
原文:零元学Expression Blend 4 - Chapter 3 熟悉操作第一步(制作一个猴子脸) 本篇内容会教你如何使用笔刷.钢笔.渐层以及透明度的调整,还有如何转化图层和路径,最重要的是要 ...
- PING[ARC2]: Heartbeat failed to connect to standby 'gqtzdb_dg'. Error is 16047
Data Guard搭建完毕之后,执行开启归档路径2,结果发现不同步, SQL> alter system set log_archive_dest_state_2=enable; 查看错误日志 ...