多校第六场(01.03打表)

01.A boring Question

题意:求题目中给的公式的值,具体见题目。


分析:一开始是不会做的,后来打了一下表,可以观察出规律 a(n+1)=an*m+1.可得到公式(m^(n+1)-1)/(m-1),所以两次快速幂一次求幂一次求逆元就好了,记得取模。有不足的请指正。

#include <stdio.h>
#define LL long long
const int mod=1e9+7;
inline LL quick_inverse(LL n, LL p) {
    LL ret = 1,exponent = p;
    for (LL i = exponent; i; i >>= 1, n = n * n % mod) {
        if (i & 1) {
            ret = ret * n % mod;
        }
    } 
    return ret;
}

int main()
{
    LL t,n,m;
    for(scanf("%I64d",&t);t--;)
    {
        scanf("%I64d%I64d",&n,&m);
        printf("%I64d
",((quick_inverse(m,n+1)-1)*quick_inverse(m-1,mod-2)%mod+mod)%mod);
    }
    return 0;
}

03.A simple Nim

题意:两个人从n堆中取石子,可以取任意颗也可以将一堆分成三堆(n>=3),二选一,问最后谁赢。

分析:打表找出规律,数学归纳法可得出。(我觉得题目——还是我太蒻了)

#include<stdio.h>
int main()
{
    int T,i,res,x,n;
    scanf("%d",&T);
    while(T--)
    {
        res=0;
        scanf("%d",&n);
        for(i=0;i<n;++i)
        {
            scanf("%d",&x);
            if(!(x%8))
                res=res^(x-1);
            else if(x%8==7)
                res=res^(x+1);
            else
                res=res^(x);
        }
        if(res)
            printf("First player wins.
");
        else
            printf("Second player wins.
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chendl111/p/5738178.html