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.
The single line of input contains one integer n (0 ≤ n ≤ 109).
Print single integer — the last digit of 1378n.
1
8
2
4
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));
}
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.
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.
Print a single integer: the answer to the problem.
2 3 1 2
1
6 1 5 1 2 3 4 1
2
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 = 3, j = 4 (since ) and i = 1, j = 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);
}
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 y, x 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).
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.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
4 2 3 1 4
3
4 4 4 4 4
-1
4 2 1 4 3
1
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.