原题链接: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 路由 前两章节中,我们提到 ASP.NET Core 支持 MVC 开发 ...

  2. hdu3118Arbiter (使用二分图的定义,枚举每个状态)

    Arbiter Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Sub ...

  3. WPF教程002 - 实现Step步骤条控件

    原文:WPF教程002 - 实现Step步骤条控件 在网上看到这么一个效果,刚好在用WPF做控件,就想着用WPF来实现一下 1.实现原理 1.1.该控件分为2个模块,类似ComboBox控件分为Ste ...

  4. WPF MessageBox 添加确认取消按钮 并判断

    很简单的功能随笔 if (System.Windows.MessageBox.Show("您确定要删除吗?", "提示:", MessageBoxButton. ...

  5. WPF 自定义范围分组

    <Window x:Class="ViewExam.MainWindow"        xmlns="http://schemas.microsoft.com/w ...

  6. IIS Express 启用目录浏览

    IIS Express 启用目录浏览,有需要的朋友可以参考下. 今天刚刚使用visual studio 2013创建第一个hello world,结果就发现提示错误. HTTP 错误 403.14 - ...

  7. RelativeSource 简述

    原文:RelativeSource 简述 RelativeSource实现标记扩展,以描述绑定源相对于绑定目标的位置. <Binding> <Binding.RelativeSour ...

  8. python 编码转换 专题

    主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...

  9. Win10如何设置开机自动登录

    原文:Win10如何设置开机自动登录 第一步: 小娜搜索"netplwiz",进入用户账户设置. 第二步: 先勾选选中一次,要使用本计算机,用户必须输入用户名和密码. 第三步: 取 ...

  10. Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效

    原文:Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效 /// <summary> /// Image merge process. /// </summar ...