Codeforces Round #383 (Div. 2)

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.


题意:求整数幂对10的取模,简单快速幂,我却先耿直tle一发,然后立马换了我上篇博客的套路,其实直接快速幂的裸题啊,贴上快速幂的代码吧:


(直接规律也行,四个一循环,n=0时是1,后面8、4、2、6)

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

int pow_mod(int a,int b,int p)
{
    int ans = 1,base = a;
    while(b)
    {
        if(b&1)
            ans = ans*base%p;
        base = base*base%p;
        b >>= 1;
    }
    return ans;
}
int main()
{
    int n;
    cin >> n;
    printf("%d\n",pow_mod(1378,n,10));
}

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.

求数组中a[i]^a[j]=x的i、j对数

卡了,异或的性质  k^kk = x   ,  k^x = kk,所以直接拿输入k找其异或x的值的数量就行了。

要开long long,对数会爆int

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

//k^kk = x ,k^x = kk;
int a[1000000+5];
int main()
{
    int n,x,k;
    long long sum = 0;
    cin >> n >> x;
    for(int i = 1;i <= n;i++)
    {
        //scanf("%d",&a[i]);
        scanf("%d",&k);
        sum += a[k^x];
        a[k]++;
    }

    /*sort(a+1,a+n+1);
    for(int i = 1;i <= n;i++)
    {
        int low = i+1,high = n,mid;
        while(low <= high)
        {
            mid = (low+high)/2;
            if((a[mid]^a[i]) > x)
                high = mid-1;
            else if((a[mid]^a[i]) < x)
                low = mid+1;
            else
            {
                sum++;
            }
        }
    }
    */
    printf("%lld\n",sum);
}

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.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值