2300. 【noip普及组第一题】模板题

(File IO): input:template.in output:template.out

时间限制: 1000 ms  空间限制: 262144 KB  具体限制

题目描述

输入

输出

样例输入

样例输出

数据范围限制

朴素算法

考试开始的前一个小时我一直在折腾朴素算法 -> 对拍

 #pragma GCC optimize(2)
#include<bits/stdc++.h>
#define IL inline
using namespace std;
int a[];
bool vis[];
int n,k,maxans;
bool cmp(int a,int b)
{
return a>b;
}
int gcd(int a,int b)
{
if(b==) return a;
if(b==) return ;
if(a%==&&b%==) return *gcd(a/,b/);
if(a%==&&b%==) return gcd(a,b/);
if(a%==&&b%==) return gcd(a/,b);
if(a%==&&b%==) return gcd(b,a%b);
// return (b==0)?a:gcd(b,a%b);
//这里还用到了二进制gcd(会更快一点)
}
void search(int depth/*k*/,int now)
{
if(depth==k) {
maxans=max(maxans,now);
return;
}
int maxgcd=,maxnum=;
for(int i=;i<=n;i++)
{
if(vis[i]) continue;
if(gcd(now,a[i])>maxgcd){
maxnum=i;
maxgcd=gcd(now,a[i]);
if(depth+<k){
vis[i]=;
search(depth+,maxgcd);
vis[i]=;
}
}
if(depth+==k)
{
vis[i]=;
search(depth+,maxgcd);
vis[i]=;
}
}
}
int main()
{
freopen("template.in","r",stdin);
freopen("template.out","w",stdout);
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",a+i);
sort(a+,a+n+,cmp);
for(k=;k<=n;k++)
{
if(a[k]==){
printf("1\n");
continue;
}
maxans=;
for(int s=;s<=n;s++)
{
vis[s]=;
search(,a[s]);
vis[s]=;
}
printf("%d\n",maxans);
}
return ;
}

我再也不想看到了

这个算法就是模拟,谁都会写吧?

O(n2)算法

我会写出这个算法来,完全是因为下面的那一种在考试时我写出来有问题

Solution

先在输入的同时预处理出每个数的所有因数(可以是质数,合数,也可以是1)

for(int j=;j<a;j++)
if(a%j==)
array[j]++;

这样子的好处是,我没有保存每一个数,而是统计了所有的因子

这里的array[i]就表示因子有i的数有多少个

于是我们要计算 10,000个数 * 10,000个可能的因子 次询问

哈哈

还是把这种讲完吧

然后再外层循环k++

内层循环i--

一旦有一个array[i]>=k

就把这时的i输出即可

Code(TLE70分)

 //#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
int array[];
int a,n,k,maxa;
int main()
{
freopen("template.in","r",stdin);
freopen("template.out","w",stdout);
cin>>n;
for(int i=;i<=n;i++){
scanf("%d",&a);
for(int j=;j<a;j++)
if(a%j==)
array[j]++;
maxa=max(maxa,a);
}
int b=maxa;
for(k=;k<=n;k++)
{
for(int i=b;i>;i--)
{
if(array[i]>=k){
cout<<i<<endl;
b=i;
break;
}
}
}
return ;
}

Code(TLE70分)

O(n sqrt(n))算法

Solution

纪中10日T1 2300. 【noip普及组第一题】模板题的更多相关文章

  1. 纪中10日T1 2313. 动态仙人掌

    纪中10日 2313. 动态仙人掌 (File IO): input:dinosaur.in output:dinosaur.out 时间限制: 1500 ms  空间限制: 524288 KB  具 ...

  2. 纪中17日T1 2321. 方程

    纪中17日T1 2321. 方程 (File IO): input:cti.in output:cti.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto ...

  3. 2016.9.10初中部上午NOIP普及组比赛总结

    2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...

  4. noip普及组考纲+样题合集——初级篇(OIer必看)

    很明显我是想发提高组合集的.普及组考纲……用发么. 当然如果你想看的话也可以,就一点点: 递归.排序…… 很明显上面那都不是重点.普及组只要掌握搜索.二分.单调队列.数学.随机化等等,一等奖没问题的, ...

  5. 纪中5日T1 1564. 旅游

    1564. 旅游 题目描述 输入N个数,从中选择一些出来计算出总和,问有多少种选法使得和为质数. 输入 第一行一个整数N. 第二行N个整数,表示这N个数的值. 输出 一个整数,表示方案数. 样例输入 ...

  6. 纪中12日T1 2307. 选择

    2307. 选择 (File IO): input:choose.in output:choose.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto Pr ...

  7. 纪中10日T3 2296. 神殿 bfs

    2296. 神殿 (File IO): input:temple.in output:temple.out 时间限制: 1500 ms  空间限制: 524288 KB  具体限制 Goto Prob ...

  8. 2016.8.15上午纪中初中部NOIP普及组比赛

    2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...

  9. 2016.8.17上午纪中初中部NOIP普及组比赛

    2016.8.17上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1335 本来觉得自己能考高分,但只得160分,并列第九.至少又挤 ...

随机推荐

  1. React使用antd按需引入报错

    引言 按照antd官网配置按需引入,还是出现一系列的报错: 原因 在网上搜了一下,大部分说是react-scripts以及react-app-rewired版本不兼容的问题,我果断把下载低版本 npm ...

  2. 1、通过CP数据文件的方式恢复MySQL 从库 启动后报错:Last_IO_Errno: 1236:A slave with the same server_uuid/server_id as this slave has connected to the master;

    1.问题: MySQL从库中查看主从状态: show slave status\G,发现出现IO的报错: Last_IO_Errno: Last_IO_Error: Got fatal error f ...

  3. FFMPEG学习----使用SDL播放YUV数据

    命令行下配置: G:\Coding\Video\SDL\proj>tree /F 文件夹 PATH 列表 卷序列号为 0FD5-0CC8 G:. │ sdl.cpp │ SDL2.dll │ S ...

  4. Pandas Statistical Functions

    import pandas as pd import random import numpy as np n_rows=5 n_cols=2 df = pd.DataFrame(np.random.r ...

  5. pandas 使用总结

    import pandas as pd import numpy as np ## 从字典初始化df ipl_data = {'Team': ['Riders', 'Riders', 'Devils' ...

  6. 题解 CF1292A 【NEKO's Maze Game】

    有一个结论: 当 \((1,1)\) 不能抵达 \((2,n)\) 时,必定存在一个点对,这两个点的值均为真,且坐标中的 \(x\) 互异,\(y\) 的差 \(\leq 1\) 这个结论的正确性感觉 ...

  7. Qt 中 this->size() this->rect() event->size() 三者差异

    测试代码: void OsgEarthGraphicsView::resizeEvent(QResizeEvent* event) { //if (scene()) //{ // scene()-&g ...

  8. Source Code Structure - Python 源码目录结构

    Source Code Structure - Python 源码目录结构 Include 目录包含了 Python 提供的所有头文件, 如果用户需要用 C 或 C++ 编写自定义模块扩展 Pytho ...

  9. 浏览器中的 .Net Core —— Blazor WebAssembly 初体验

    前言 在两年多以前就听闻 Blazor 框架,是 .Net 之父的业余实验性项目,其目的是探索 .Net 与 WebAssembly 的兼容性和应用前景.现在这个项目已经正式成为 Asp.Net Co ...

  10. hive执行计划简单分析

    原始SQL: select a2.ISSUE_CODE as ISSUE_CODE, a2.FZQDM as FZQDM, a2.FZQLB as FZQLB, a2.FJJDM as FJJDM, ...