Asia Hong Kong Regional Contest 2016 Playing with Numbers

本文解析了一道关于数论操作的编程竞赛题目,探讨了如何通过取对数排序的方法简化问题,快速找到最大和最小可能的运算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:

https://open.kattis.com/problems/playingwithnumbers

You have a list of  N N numbers, each of the form  2a3b 2a3b for some non-negative integers  a aand  b b. You want to perform  N1 N−1 operations on these numbers. Each operation acts on two numbers  X X and  Y Y of your choice from the list, replacing them with a new number  op(X,Y) op(X,Y). After each operation, your list has one fewer number.

In this task, an operation  op op can be  gcd gcd or  lcm lcm (they stand for greatest common divisor and least common multiple, respectively). There are a total of  N N scenarios: You may apply “ gcd gcd” operations  k k times and “ lcm lcm” operations  N1k N−1−k times. For each of the  N N scenarios, what is the largest possible outcome after these  N1 N−1operations? What about the smallest possible outcome?

Input

The first line consists of a single integer  N N ( 1N50000 1≤N≤50000). The following  N Nlines each contains a pair of integers  ai ai and  bi bi ( 0ai,bi1000 0≤ai,bi≤1000), indicating that the  i ith number in your initial list is  2ai3bi 2ai3bi.

Output

Output  N N lines in total. On line  i i ( i=1,,N i=1,…,N), output four space-separated integers  a,b,a a,b,a′ and  b b′. The first pair of integers  a a and  b b indicate that the largest possible outcome is  2a3b 2a3b with  i1 i−1 “ gcd gcd” operations (and therefore  Ni N−i “ lcm lcm” operations). The second pair of integers  a a′ and  b b′ indicate that the smallest possible outcome is  2a3b 2a′3b′, again with  i1 i−1 “ gcd gcd” operations.

Explanation for sample data

The three numbers are  2030=1 2030=1 2132=18 2132=18, and  2230=4 2230=4.

  1. When  i=0 i=0, we can only take  lcm lcm lcm(1,18,4)=36=2232 lcm(1,18,4)=36=2232.

  2. When  i=1 i=1, the largest outcome is  lcm(18,gcd(1,4))=18=2132 lcm(18,gcd(1,4))=18=2132, and the smallest outcome is  gcd(1,lcm(18,4))=1=2030 gcd(1,lcm(18,4))=1=2030.

  3. When  i=2 i=2, we can only take  gcd gcd gcd(1,18,4)=1=2030 gcd(1,18,4)=1=2030.

Sample Input 1Sample Output 1
3
0 0
1 2
2 0
2 2 2 2
1 2 0 0
0 0 0 0


 取对数排序,只需要考虑第一第二和倒数第一第二的输出


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <sstream>
#include <climits>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define pi acos(-1.0)
#define INF 2147483647
using namespace std;
typedef long long ll;
typedef pair <int,int > P;
int N,i,j;
struct Number
{
    int a,b;
    double m;
} number[50005];
bool cmp(const Number&q,const Number&w)
{
    return q.m<w.m;
}
int main()
{
    scanf("%d",&N);
    memset(number,0,sizeof(number));
    int a_min=INF,b_min=INF,a_max=-1,b_max=-1;
    for(i=0; i<N; i++)
    {
        scanf("%d%d",&number[i].a,&number[i].b);
        a_min=min(a_min,number[i].a);
        a_max=max(a_max,number[i].a);
        b_min=min(b_min,number[i].b);
        b_max=max(b_max,number[i].b);
        double flag=log2(3);
        number[i].m=flag*number[i].b+number[i].a;
    }
    sort(number,number+N,cmp);
    printf("%d %d %d %d\n",a_max,b_max,a_max,b_max);
    for(i=1; i<N-1; i++)
    {
        if(i==N-2)
        {
            printf("%d %d %d %d\n",number[N-1].a,number[N-1].b,a_min,b_min);
        }
        else if(i==1)
        {
            printf("%d %d %d %d\n",a_max,b_max,number[0].a,number[0].b);
        }
        else
            printf("%d %d %d %d\n",a_max,b_max,a_min,b_min);
    }
    if(N>1)
        printf("%d %d %d %d\n",a_min,b_min,a_min,b_min);
    return 0;
}

### 参与ICPC亚洲日本赛区线上第一轮比赛的信息和规则 #### 比赛概述 国际大学生程序设计竞赛(International Collegiate Programming Contest, ICPC)是一项全球性的编程竞赛活动。其中,ICPC亚洲区域赛分为多个赛区,包括中国、韩国以及日本等地的比赛。对于希望参加ICPC Asia Japan Online First-Round Contest的学生来说,了解具体的参赛信息至关重要。 #### 报名条件 通常情况下,只有在校本科生或者研究生可以组队报名参加此类赛事[^1]。每支队伍由三名队员组成,并且需要指定一名指导教师负责监督整个过程。需要注意的是,不同年份的具体规定可能会有所变化,因此建议关注官方发布的最新通知来获取最准确的要求。 #### 注册流程 为了能够顺利参与到比赛中去,团队应该尽早完成在线注册手续。这一般涉及到填写基本信息表格并提交给主办方审核通过之后才能正式成为参赛者之一。此外,在线平台还会提供练习环境供选手熟悉系统操作方式及其功能特性[^2]。 #### 赛事形式 该类竞赛多采用限时解题的形式来进行考核,即给出若干道算法题目让各小组在规定时间内解答尽可能多的问题以获得更高分数。期间允许查阅资料但严禁作弊行为的发生。值得注意的是,由于这是线上初选阶段,所以不存在现场答辩环节[^3]。 #### 准备工作 准备过程中除了要掌握扎实的数据结构与算法基础知识外,还应当注重培养良好的协作沟通能力以便于更好地发挥集体智慧解决问题。另外也可以参考往届真题集锦加深理解常见考点分布情况从而提高实战水平[^4]。 ```cpp // 示例代码用于展示如何处理输入输出流加速读取效率 #include<bits/stdc++.h> using namespace std; void solve(){ // 解决具体问题逻辑实现部分... } signed main(){ ios::sync_with_stdio(false); int T; cin >> T; while(T--)solve(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值