Codeforces Round 383

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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值