Codeforces Round #383 (Div. 2)全部题解 (A,B,C,D,E)

62 篇文章 2 订阅
26 篇文章 0 订阅

A. Arpa’s hard exam and Mehrdad’s naive cheat
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do.

Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378n.

Mehrdad has become quite confused and wants you to help him. Please help, although it's a naive cheat.

Input

The single line of input contains one integer n (0  ≤  n  ≤  109).

Output

Print single integer — the last digit of 1378n.

Examples
input
1
output
8
input
2
output
4
Note

In the first example, last digit of 13781 = 1378 is 8.

In the second example, last digit of 13782 = 1378·1378 = 1898884 is 4.

简单题 ,稍微手算一下就知道每四个一循环,然后n=0的时候特判一下

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    if(n==0){
        puts("1");
        return 0;
    }
    int x=n%4;
    if(x==1){
        puts("8");
    }
    if(x==2){
        puts("4");
    }
    if(x==3)
        puts("2");
    if(x==0)
        puts("6");
    return 0;
}


B. Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are some beautiful girls in Arpa’s land as mentioned before.

Once Arpa came up with an obvious problem:

Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where  is bitwise xoroperation (see notes for explanation).

Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.

Input

First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x.

Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.

Output

Print a single integer: the answer to the problem.

Examples
input
2 3
1 2
output
1
input
6 1
5 1 2 3 4 1
output
2
Note

In the first sample there is only one pair of i = 1 and j = 2 so the answer is 1.

In the second sample the only two pairs are i = 3j = 4 (since ) and i = 1j = 5 (since ).

A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.


简单查找,每加入一个数,在已经加入的数里查找是否有需要的值即可。用map记录一下。


C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated ttimes) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from yx would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

Input

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
input
4
2 3 1 4
output
3
input
4
4 4 4 4
output
-1
input
4
2 1 4 3
output
1
Note

In the first sample suppose t = 3.

If the first person starts some round:

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.

The process is similar for the second and the third person.

If the fourth person starts some round:

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.


很多个人,一次给对应的人打电话。第t个人就是第一个人的Joon-Joon。问t最小为多少可以让每两个人互为Joon-Joon。

其实是个数论题,对于每个人,找到其循环节是多少。然后求最小公倍数即可

注意可能会爆int,要用long long


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
const int MAXN=110;
int a[MAXN];
int n;
long long gcd(long long a,long long b){
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

long long lcm(long long a,long long b){
    return a*b/(gcd(a,b));
}

int cnt(int x){
    int res=1;
    int temp=a[x];
    while(temp!=x){
        res++;
        temp=a[temp];
        if(res>2*n)
            break;
    }
    if(res>2*n){
        return -1;
    }
    if(res%2==0)
        return res/2;
    else
        return res;
}
int b[MAXN];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",a+i);
    }
    for(int i=1;i<=n;i++){
        b[i]=cnt(i);
        if(b[i]==-1){
            puts("-1");
            return 0;
        }
    }
    if(n==1){
        printf("1\n");
        return 0;
    }
    long long res=lcm(b[1],b[2]);
    for(int i=3;i<=n;i++){
        res=lcm(res,b[i]);
    }
    printf("%I64d\n",res);
}



D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Just to remind, girls in Arpa's land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.

Input

The first line contains integers nm and w (1  ≤  n  ≤  10001 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), meaning that Hoses xiand yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output

Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.

Examples
input
3 1 5
3 2 5
2 4 2
1 2
output
6
input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
output
7
Note

In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.



其实就是一个分组背包问题,注意因为也可以选择一个组里所有的牛。所以需要在每个组里加入一个值,等于组中所有牛的重量和魅力相加。


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
const int MAXN=11100;
int n,m,W;
long long w[MAXN];
long long b[MAXN];
vector <int> G[MAXN];
int par[MAXN];//父亲
int r[MAXN];//树的高度
//初始化n个元素
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        par[i]=i;
        r[i]=0;
    }
}
//查询数的根
int find(int x)
{
    if(par[x]==x)
        return x;
    else
        return par[x]=find(par[x]);
}
//合并x和y所属的集合
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)
        return;
    if(r[x]<r[y])
        par[x]=y;
    else
        par[y]=x;
    if(r[x]==r[y])
        r[x]++;
}

bool same(int x,int y)
{
    return find(x)==find(y);
}

long long f[MAXN];
int a[MAXN];
int main()
{
    memset(f,0,sizeof(f));
    scanf("%d%d%d",&n,&m,&W);
    for(int i=1;i<=n;i++)
        scanf("%I64d",w+i);
    for(int i=1;i<=n;i++)
        scanf("%I64d",b+i);
    int num=0;
    init(n);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        unite(x,y);
    }
    memset(a,-1,sizeof(a));
    for(int i=1;i<=n;i++){
        if(a[find(i)]==-1){
            a[find(i)]=num++;
        }
        G[a[find(i)]].push_back(i);
    }
    for(int i=0;i<num;i++){
        long long x=0,y=0;
        if(G[i].size()>1){
        for(int j=0;j<G[i].size();j++){
            x+=w[G[i][j]];
            y+=b[G[i][j]];
        }
        n++;
        w[n]=x;
        b[n]=y;
        G[i].push_back(n);
        }
    }
    for(int k=0;k<num;k++){
        for(int v=W;v>=0;v--){
            for(int i=0;i<G[k].size();i++){
                int temp=G[k][i];
                if(v>=w[temp])
                    f[v]=max(f[v],f[v-w[temp]]+b[temp]);
            }
        }
    }
    long long MAX=0;
    for(int i=0;i<=W;i++){
        MAX=max(MAX,f[i]);
    }
    printf("%I64d\n",MAX);
}

E. Arpa’s overnight party and Mehrdad’s silent entering
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Note that girls in Arpa’s land are really attractive.

Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs were numbered 1through 2n in clockwise direction. There was exactly one person sitting on each chair.

There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that:

  • Each person had exactly one type of food,
  • No boy had the same type of food as his girlfriend,
  • Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive.

Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.

Input

The first line contains an integer n (1  ≤  n  ≤  105) — the number of pairs of guests.

The i-th of the next n lines contains a pair of integers ai and bi (1  ≤ ai, bi ≤  2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.

Output

If there is no solution, print -1.

Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2.

If there are multiple solutions, print any of them.

Example
input
3
1 4
2 5
3 6
output
1 2
2 1
1 2


男女朋友之间食物不能相同,然后每相邻的三个人中两个人食物不同

很容易想到男女朋友之间要连边,那么怎么连边能够满足另一个要求?

贪心的做法是把2*i和2*i-1之间连边,这样肯定是一个二分图。然后就是简单的染色问题了。


#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int MAXN=400010;
int a[MAXN];
int b[MAXN],g[MAXN];
int n;
vector <int> G[MAXN];

bool dfs(int v,int c){
    a[v]=c;
    for(int i=0;i<G[v].size();i++){
        if(a[G[v][i]]==c)
            return false;
        if(a[G[v][i]]==0&&!dfs(G[v][i],3-c))
            return false;
    }
    return true;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        G[2*i].push_back(2*i-1);
        G[2*i-1].push_back(2*i);
        scanf("%d%d",b+i,g+i);
        G[b[i]].push_back(g[i]);
        G[g[i]].push_back(b[i]);
    }
    memset(a,0,sizeof(a));
    for(int i=1;i<=2*n;i++){
        if(a[i]==0){
            if(!dfs(i,1)){
                puts("-1");
                return 0;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",a[b[i]],a[g[i]]);
    }
    return 0;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值