Codeforces Round #628 (Div. 2)

A.
A. EhAb AnD gCd
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a positive integer x. Find any such 2 positive integers a and b such that GCD(a,b)+LCM(a,b)=x.

As a reminder, GCD(a,b) is the greatest integer that divides both a and b. Similarly, LCM(a,b) is the smallest integer such that both a and b divide it.

It’s guaranteed that the solution always exists. If there are several such pairs (a,b), you can output any of them.

Input
The first line contains a single integer t (1≤t≤100) — the number of testcases.

Each testcase consists of one line containing a single integer, x (2≤x≤109).

Output
For each testcase, output a pair of positive integers a and b (1≤a,b≤109) such that GCD(a,b)+LCM(a,b)=x. It’s guaranteed that the solution always exists. If there are several such pairs (a,b), you can output any of them.

Example
inputCopy
2
2
14
outputCopy
1 1
6 4
Note
In the first testcase of the sample, GCD(1,1)+LCM(1,1)=1+1=2.

In the second testcase of the sample, GCD(6,4)+LCM(6,4)=2+12=14.
题意:给你一个n 输出两个小于n的数a,b使得 gcd(a,b) + lcm(a,b) = n
思路:gcd是最大公约数 lcm是最小公倍数 很明显 只要输出 1 和 n - 1就是答案 gcd(a,b) = 1 lcm = n - 1

比赛的时候写的麻烦了点 还分了下奇偶性 其实都是这个规律

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second

const int N = 1e5+5;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;


int main()
{
    int t;

    cin >> t;

    while(t --)
    {
        LL x;

        cin >> x;

        if(x%2)
            cout << '1' << ' ' <<x - 1 << '\n';
        else
            cout << x / 2 << ' ' << x/2 << '\n';
    }
    return 0;
}



B. CopyCopyCopyCopyCopy
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ehab has an array a of length n. He has just enough free time to make a new array consisting of n copies of the old array, written back-to-back. What will be the length of the new array’s longest increasing subsequence?

A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order.

Input
The first line contains an integer t — the number of test cases you need to solve. The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤105) — the number of elements in the array a.

The second line contains n space-separated integers a1, a2, …, an (1≤ai≤109) — the elements of the array a.

The sum of n across the test cases doesn’t exceed 105.

Output
For each testcase, output the length of the longest increasing subsequence of a if you concatenate it to itself n times.

Example
inputCopy
2
3
3 2 1
6
3 1 4 1 5 9
outputCopy
3
5
Note
In the first sample, the new array is [3,2,1,3,2,1,3,2,1]. The longest increasing subsequence is marked in bold.

In the second sample, the longest increasing subsequence will be [1,3,4,5,9].

题意:给你n个数 问能构造出最长的递增序列是多长
思路:数就那么几个 而且要严格递增 自然就是计算一共有多少个数 不能有重复 mp标记一下就好

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second

const int N = 1e5+5;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;

int a[N];

int main()
{
    int t;

    cin >> t;

    while(t --)
    {
        int n;
        cin >> n;

        map <int,bool> mp;

        for(int i = 0;i < n;i ++ )
            cin >> a[i],mp[a[i]] = 1;



        int res = 0;
        int cnt = 0;

       map <int,bool> :: iterator  it;

        for(it = mp.begin();it != mp.end();it ++)
        {
            if(it->second!= 0)
                res ++ ;
        }

        cout << res << '\n';
    }
    return 0;
}



C. Ehab and Path-etic MEXs
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a tree consisting of n nodes. You want to write some labels on the tree’s edges such that the following conditions hold:

Every label is an integer between 0 and n−2 inclusive.
All the written labels are distinct.
The largest value among MEX(u,v) over all pairs of nodes (u,v) is as small as possible.
Here, MEX(u,v) denotes the smallest non-negative integer that isn’t written on any edge on the unique simple path from node u to node v.

Input
The first line contains the integer n (2≤n≤105) — the number of nodes in the tree.

Each of the next n−1 lines contains two space-separated integers u and v (1≤u,v≤n) that mean there’s an edge between nodes u and v. It’s guaranteed that the given graph is a tree.

Output
Output n−1 integers. The ith of them will be the number written on the ith edge (in the input order).

Examples
inputCopy
3
1 2
1 3
outputCopy
0
1
inputCopy
6
1 2
1 3
2 4
2 5
5 6
outputCopy
0
3
2
4
1
Note
The tree from the second sample:

题意:给边赋值 使得两结点之间 未出现的值Max(u,v) 最小
思路:考虑贪心 我们只要将结点度数大于2的记录位置 从最小 也就是0开始赋值 其余的随便赋值就是最优解

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define fi first
#define se second

const int N = 1e5+5;

pair <int,int> a[N];
int de[N];

int main()
{
    int n;

    scanf("%d",&n);

    for(int i = 1;i < n;i ++)
    {
        scanf("%d%d",&a[i].fi,&a[i].se);
        de[a[i].fi] ++ ;
        de[a[i].se] ++ ;
    }

    int pos = 0;

    for(int i = 1;i < n;i ++)
    {
        if(de[i] > 2)
            pos = i;
    }

    int tot = 0;
    int cnt = n - 2;

    for(int i = 1;i < n;i ++)
    {
        if(a[i].fi == pos || a[i].se == pos)
        {
            printf("%d\n",tot);
            tot ++ ;
        }
        else
        {
            printf("%d\n",cnt);
            cnt -- ;
        }
    }

    return 0;
}

Given 2 integers u and v, find the shortest array such that bitwise-xor of its elements is u, and the sum of its elements is v.

Input
The only line contains 2 integers u and v (0≤u,v≤1018).

Output
If there’s no array that satisfies the condition, print “-1”. Otherwise:

The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any.

Examples
inputCopy
2 4
outputCopy
2
3 1
inputCopy
1 3
outputCopy
3
1 1 1
inputCopy
8 5
outputCopy
-1
inputCopy
0 0
outputCopy
0
Note
In the first sample, 3⊕1=2 and 3+1=4. There is no valid array of smaller length.

Notice that in the fourth sample the array is empty.

题意:给你两个数u和v 让你构造出一个最短的数组 使得 数组内的和为v 异或和为u 不存在的话输出-1
思路:1.首先很容易得到如果u>v 和 u v的奇偶性不同的话 (最低位的0 和 1是没有办法处理的) 是肯定不存在的 输出-1
2.如果u = v 如果u = 0 输出序列长度为0就好 u !=0 那就是序列长度为1 元素就是它自己本身
3.
在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define fi first
#define se second

int main()
{
    LL u,v;

    cin >> u >> v;

    if(u > v || (u + v)&1)
    {
        printf("-1\n");
        return 0;
    }

    if(u == v)
    {
        if(u == 0)
            printf("0\n");
        else
            printf("1\n%lld",u);
        return 0;
    }

    LL x = (v - u)/2;

    if(u&x)
        printf("3\n%lld %lld %lld",u,x,x);
    else
        printf("2\n%lld %lld",(u + x),x);

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值