A Arpa’s hard exam and Mehrdad’s naive cheat
求
1378n
个位是几。找规律。。。。
#include <cstdio>
#include <cstring>
const int d[]={6,8,4,2};
int main(){
int n;
while(~scanf("%d",&n)){
if(n==0)printf("1\n");
else{
printf("%d\n",d[n%4]);
}
}
return 0;
}
B Arpa’s obvious problem and Mehrdad’s terrible solution
找有多少对 a[i] xor a[j] == x.
用map记录 x^a[i] 值的个数。因为异或的自反性, x^a[i]^a[i]==x
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define LL long long
using namespace std;
int n,x;
int a;
map<int,int> mp;
int main(){
scanf("%d%d",&n,&x);
mp.clear();
LL ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&a);
if(mp[x^a]) ans+=mp[x^a];
mp[a]++;
}
cout<<ans<<endl;
return 0;
}
C Arpa’s loud Owf and Mehrdad’s evil plan
找f[f[….f[x]…]]=y&&f[f[…f[y]…]]=x,t为嵌套的个数。
要求所有t的最小公倍数?
t为偶数的话可以除以2,然后一起求。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <climits>
#define LL long long
using namespace std;
int a[1010];
int len[1010];
LL gcd(LL a,LL b){
return b==0?a:gcd(b,a%b);
}
LL lcm(LL a,LL b){
return a*b/gcd(a,b);
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=n;i++){
int t=1,x=a[i];
while(1){
x=a[x];
if(x==i){
t++;
if(t%2==0) t=t/2;
len[i]=t;
break;
}
t++;
if(t>n) break;
}
}
LL ans=0;
for(int i=1;i<=n;i++){
if(len[i]==0){
ans=-1;
break;
}
if(ans==0){
ans = len[i];
continue;
}
ans = lcm(ans,len[i]);
}
cout<<ans<<endl;
return 0;
}