【Codeforces Round #383 (Div. 2)】 (A,B,C)

来源:互联网 发布:sl会员商城源码 编辑:程序博客网 时间:2024/05/16 18:11
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.

题意:求1378的N次方的最后一位数是多少

思路:两种方法,一个快速幂,一个找规律,我这用的快速幂

#include<cstdio>int exgcd(int a,int b,int mod){int ans = 1;while(b){if(b & 1)    ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans % mod;}int main(){int n;while(~scanf("%d",&n)){printf("%d\n",exgcd(8,n,10));}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 31 2
output
1
input
6 15 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,其中1 ≤ i < j ≤ n

思路:由于已知x,当枚举a[i]时,a[j]的值是唯一的,所以可以用一个数组b[]来保存数字串中各数字出现的次数,当枚举到a[i]时,设k = x ^ a[i],那么ans应该加上在a[i]之后的所有x的个数

#include<cstdio>#include<cstring>using namespace std;const int maxn = 1e5 + 10;int a[maxn];int b[maxn];int main(){int n,x;scanf("%d%d",&n,&x);memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(int i = 0; i < n; i++){scanf("%d",&a[i]);b[a[i]]++;}__int64 ans = 0; // 注意ans的值有可能会超intfor(int i = 0; i < n; i++){b[a[i]]--;int k = x ^ a[i];if(k > 1e5) // 当k > 1e5 时,在数字串中不可能存在一对使得满足条件   continue;ans += b[k];}printf("%I64d\n",ans);return 0;}

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 repeatedt 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 theJoon-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
42 3 1 4
output
3
input
44 4 4 4
output
-1
input
42 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 is1.

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的值使得从任意一个人开始后能经过t次回到出发点

思路:从任意点开始,假设从x -> y,再从y -> x,这样必定存在一个环,所以枚举找出从每个点开始再回到自身的最小环M,当M的值为偶数时,M的最小环为 M / 2,再对所有最小环求最小公倍数

#include<cstdio>#include<cstring>using namespace std;typedef __int64 LL;const int maxn = 110;int a[maxn];bool falg[maxn];int ma[maxn];LL gcd(LL n,LL m){    if(m == 0)        return n;    return gcd(m,n%m);}int main(){    int n;    scanf("%d",&n);    for(int i = 1; i <= n; i++)        scanf("%d",&a[i]);    memset(ma,-1,sizeof(ma));    for(int i = 1; i <= n; i++)    {        int t = 0;        memset(falg,false,sizeof(falg));        int v = a[i];        while(!falg[v])        {            t++;            falg[v] = true;            v = a[v];            if(v == i)            {                t++;                ma[i] = t;                break;            }        }    }    LL ans = 1;bool F = false;    for(int  i = 1; i <= n; i++){        if(ma[i] == -1)            F = true;        else        {            if(ma[i] % 2 == 0)                ma[i] /= 2;           LL k = gcd(ans,ma[i]);           ans = ans / k * ma[i];        }    }    if(F)        printf("-1\n");    else        printf("%I64d\n",ans);    return 0;}



0 0
原创粉丝点击