原题链接: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!的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  3. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  4. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  5. 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 ...

  6. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  7. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  8. code forces Jeff and Periods

    /* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...

  9. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 登录登出 上一章节我们总算完善了注册的功能,而且也添加了一个用户,现 ...

  2. MCB2300的CTM1050(CAN) - 系列示意图

    这一系列示意图由Portel DXP 2004绘. 截图: 文件下载: CTM1050.7z 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  3. LeapMotion Demo1

    原文:LeapMotion Demo1     LeapMotion SDK For c# 只提供了一个Sample.cs.   Leap Motion App Home 可以给初入手者提供很好的用户 ...

  4. WPF SystemParameters的使用

    SubWindow win = new SubWindow();                //win.Width = SystemParameters.WorkArea.Width;      ...

  5. asp .net core 读取读取Views文件夹下的js和css

    //读取Views文件夹下的js和css app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFilePro ...

  6. glibc内存管理方式

    程序员接触的内存空间和系统接触的物理内存空间是有所区别的.对于一般进程来讲,他面对的是一个线性虚拟内存空间:地址从0到最大值.每一个进程面对的虚拟内存空间都是一样的,都享有全部的内存地址.虚拟内存空间 ...

  7. js 动态生成div显示id

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  8. 在 __CC_ARM 编译器环境下,使用$Sub$$ 与 $Super$$ 的“补丁”功能

    $Sub$$ 与 $Super$$ 的“补丁”功能(详见 ARM® Compiler v5.06 for µVision® armlink User Guide): 这是一种特殊模式:用于有一个已经存 ...

  9. ubuntu16.04安装搜狗输入法

    安装完Ubuntu 16.04后,要更换为国内的软件源: Ali-OSM Alibaba Open Source Mirror Site Home About Join Us Ubuntu 1.软件包 ...

  10. TThreadList Demo

    type TForm1 = class(TForm) Button1: TButton; Button3: TButton; ListBox1: TListBox; Button2: TButton; ...