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 ...
随机推荐
- IOS status bar
从iOS7开始,该系统提供2样的管理风格状态栏 由UIViewController管理(每UIViewController我们可以有各自不同的状态栏) 由UIApplication管理(由其统一管理的 ...
- gcc和MinGW的异同(在cygwin/gcc做的东西可以无缝的用在linux下,没有任何问题,是在windows下开发linux程序的一个很好的选择)
cygwin/gcc和MinGW都是gcc在windows下的编译环境,但是它们有什么区别,在实际工作中如何选择这两种编译器. cygwin/gcc完全可以和在linux下的gcc化做等号,这个可以从 ...
- WPF Binding Path妙用代码实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- ios7 获取UITablleViewCell
iOS7之前UITablleViewCell中得contentView得superView就是UITableViewCell.但是在iOS7得时候,contentView得superView确实UIT ...
- iOS开发之应用首次启动显示用户引导
这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单 我们只需要在一个类里面写好用户引导页面 基本上都是使用UIScrollView 来实现, 新建一个继承于UIViewController ...
- 如何设置程序UAC控制
在做项目的过程中,有很多情况会涉及到权限问题,要求必须以管理员的身份才能运行,如何强制我们的程序必须以管理员身份运行呢?在调查的过程中发现有很多方式,此处介绍一种简单的方式. 1.在VS中,右键点击工 ...
- Motion Paths in WPF 4 using Expression Blend 4
原文 Motion Paths in WPF 4 using Expression Blend 4 Posted by: Pravinkumar Dabade , on 3/19/2011, in C ...
- Gralde 网络代理
Gralde 网络代理 Gradle在编译项目的时候,需要下载一些依赖.墙外的网络就需要设置代理了. 设置的方法,见文档: Accessing the web through a HTTP proxy ...
- Android零基础入门第37节:初识ListView
原文:Android零基础入门第37节:初识ListView 之前我们学习的一些UI组件都比较简单,但是在实际开发中,会经常遇见列表界面设计,如通讯录.电话列表.信息列表等.那么从本节开始来详细学习列 ...
- tf.nn.softmax & tf.nn.reduce_sum & tf.nn.softmax_cross_entropy_with_logits
tf.nn.softmax softmax是神经网络的最后一层将实数空间映射到概率空间的常用方法,公式如下: \[ softmax(x)_i=\frac{exp(x_i)}{\sum_jexp(x_j ...