Codeforces Round #383 (Div. 2)ABC

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.

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>

using namespace std;

void fun()
{
    int a=1;
     for(int i=1;i<=20;i++)
    {
        a=a*1378;
        a=a%10;
        cout<<a<<endl;
    }

}

int main()
{
    //fun();
    int n;
    int a[5]={0,8,4,2,6};

    while(~scanf("%d",&n))
    {
        if(n==0)
        {
            puts("1");
            continue;
        }
        int t=n%4;
        if(t==0) t=4;
        printf("%d\n",a[t]);
    }

    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.

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define ll __int64
using namespace std;

ll a[100009];
ll b[100090];
int main()
{
    int n;
    ll x;
    while(~scanf("%d%I64d",&n,&x))
    {
        map<int,ll>mp;
         for(int i=0;i<n;i++)
         {
              scanf("%I64d",&a[i]);
              mp[a[i]]++;
              b[i]=a[i]^x;
         }
         ll ans=0LL;

         for(int i=0;i<n;i++)
         {
             if(mp[b[i]] && b[i]==a[i]) //注意下x=0
             {
                 ll t=mp[b[i]];
                 ans+=t*(t-1)/2;
                 mp[b[i]]=0;
             }
             else if(mp[b[i]])
             {
                 ans+=mp[a[i]]*mp[b[i]];
                 mp[b[i]]=0;
             }
         }

         printf("%I64d\n",ans);

    }
    return 0;
}

/*
8 1
2 3 2 3 4 5 4 5
*/
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 t times) 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.

题意:给定的n的数,都能从自己为起点成环在回到自己,分奇偶,偶数可以两边走,所以除以2,要满足所有的周期,所以求最小公倍数。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define ll __int64
using namespace std;

int a[200];
int vis[200];
int flag;

int gcd(int a,int b)
{
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    int t=gcd(a,b);
    return a/t*b;  //注意爆int
}

void dfs(int x,int ff,int cnt)
{
    int t=a[x];
    if(t==ff)
    {
        flag=cnt;
        return;
    }
    if(vis[t] || t==x)  //中途出现环
    {
        flag=-1;
        return;
    }
    if(flag)
        return;
    vis[x]=1;
    dfs(t,ff,cnt+1);
    if(flag) return;

}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);

        int ans=-1;
        int ff=0;
        for(int i=1; i<=n; i++)
        {
            int t=a[i];
            if(t==i)
            {
                ff++;
                continue;
            }
            flag=0;
            memset(vis,0,sizeof vis);
            dfs(i,i,1);
            if(flag==-1)  //不能成环
            {
                ans=-1;
                break;
            }
            else if(flag)
            {
                if(flag%2==0) flag/=2;  //偶数环
                if(ans==-1)
                    ans=flag;
                else
                ans=lcm(ans,flag);
            }
        }
        if(ff==n)
            ans=1;

        printf("%d\n",ans);

    }

    return 0;
}
/*
4
2 3 1 4

4
4 4 4 4

4
2 1 4 3

4
2 3 4 1

10
8 10 4 3 2 1 9 6 5 7
(15)
*/




















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值