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 ...
随机推荐
- VCL to UniGUI Migration Wizard
Free Evaluation Edition of The Automatic Migration Scripting Wizard For Converting Legacy Delphi Cod ...
- 谷歌推出备份新工具:Google Drive将同步计算机文件
Google 正在将云端硬盘 Drive 转变成更强大的文件备份工具.很快,Google Drive 将能监测并备份你电脑上的(几乎)所有文件,只要是你勾选的文档,Drive 就能同步至云端. 具体来 ...
- 异步Servlet的理解与实践
AsyncContext理解 Servlet 3.0(JSR315)定义了Servlet/Filter的异步特性规范. 怎么理解"异步Servlet/Filter"及其使用情景? ...
- ArcGIS Runtime SDK for WPF 初始化
安装包 管理nuget包 Esri.ArcGISRuntime.WPF 也许还需要 Esri.ArcGISRuntime.Hydrography Esri.ArcGISRuntime.LocalSer ...
- RHEL 6和RHEL 7(CentOS 6和CentOS 7)恢复ROOT密码
RedHat 6恢复Root密码: 1.启动RedHat 6的时候在这个界面按任意键 2.出现如下界面,按 e 3.出现如下界面,选择第二个--kernel,然后再按 e 4.出现如下界面,输入 空格 ...
- ControlTemplate
ControlTemplate:外观定制 <Window.Resources> <ControlTemplate x:Key="CheckBoxControlTemplat ...
- 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlData ...
- 自动启动 Windows 10 UWP 应用
原文: https://docs.microsoft.com/zh-cn/windows/uwp/xbox-apps/automate-launching-uwp-apps 简介 开发人员有多种选项可 ...
- CentOS搭建python开发环境
装了个CentOS 5.5,想在上面搭个python的开发环境,可是还是遇到了很多问题,记录一下过程: 1.python升级 查看python版本 python -V Python 2.4.3 因为p ...
- 调用其它UI文件
调用其它UI文件 首先 要头文件包含 #include “dialog_biaozhun.h"Dialog_biaozhun *dialog = new Dialog_biaozhun() ...