比赛题解 【Educational Codeforces Round 13】

· · 个人记录

A. Johny Likes Numbers

简单计算

#include<cstdio>

int main(void)
{
    int n,k;
    scanf("%d%d",&n,&k);
    printf("%d",n+(k-n%k));
    return 0;
}

B. The Same Calendar

模拟,注意判断闰年

#include<cstdio>

inline bool leap(int y)
{
    return y%400==0 || (y%4==0 && y%100!=0);
}

int main(void)
{
    int y;
    scanf("%d",&y);

    int ans=y+1,d=0;
    for(int nd=0; ; ++ans)
    {
        nd=(nd+365+leap(ans))%7;
        if(leap(ans)!=leap(y)) continue;

        if(nd==d)
        {
            printf("%d",ans);
            return 0;
        }
    }
    return 0;
}

C. Joty and Chocolate

1到n中,能被x整除的数的个数是\lfloor \frac{n}{x} \rfloor

显然同时能被a和b整除的数,也就是能被\text{lcm}(a,b)整除的数,显然得到\max(p,q)的钱

#include<cstdio>
#include<iostream>
typedef long long ll;
using namespace std;

ll gcd(ll a,ll b){ return b? gcd(b,a%b): a;}
ll lcm(ll a,ll b){ return a*b/gcd(a,b);}

int main(void)
{
    ll n,a,b,p,q;
    cin>>n>>a>>b>>p>>q;
    cout<< n/a*p + n/b*q - n/lcm(a,b)*min(p,q);
    return 0;
}

D. Iterated Linear Function

解法1

g^{(n)}(x) &= f(g^{(n-1)}(x))\\ &= Ag^{(n-1)}(x) + B\\ &= A(Ag^{(n-2)}(x) + B) + B\\ &= A^2g^{(n-2)}(x) + AB + B\\ &= A^2(Ag^{(n-3)}(x) + B) + AB + B\\ &= A^3g^{(n-3)}(x) + A^2B + AB + B\\ &\cdots\\ &= A^ng^{(0)}(x) + B(A^{n-1} + A^{n-2} + \cdots + A^2 + A + 1) \end{aligned}

当A=1时

\text{原式}=x+Bn

当A>1时

\text{原式}=A^nx + B\times \frac{A^n-1}{A-1}

注意乘法溢出问题

#include<cstdio>
#include<iostream>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;

inline ll pw(ll a,ll b)
{
    b%=mod-1;
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void)
{
    ll a,b,n,x;
    cin>>a>>b>>n>>x;

    if(a==1) cout<<(x+b*(n%mod))%mod;
    else
    {
        ll pwa=pw(a,n);
        cout<<( pwa*x + b*( (pwa-1)*pw(a-1,mod-2) %mod) )%mod;
    }
    return 0;
}

解法2

递推可以考虑使用矩阵

对于所有i\geq 1,有

\begin{bmatrix} A & B \\ 0 & 1 \end{bmatrix} \begin{bmatrix} g_{i-1}\\ 1 \end{bmatrix} = \begin{bmatrix} g_{i}\\ 1 \end{bmatrix}

所以

\begin{bmatrix} g_n\\ 1 \end{bmatrix} = \begin{bmatrix} A & B \\ 0 & 1 \end{bmatrix}^n \begin{bmatrix} g_0\\ 1 \end{bmatrix} = \begin{bmatrix} A & B \\ 0 & 1 \end{bmatrix}^n \begin{bmatrix} x\\ 1 \end{bmatrix}

可以使用矩阵快速幂

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;

ll c[3][3];
inline void mul(ll a[3][3],ll b[3][3])
{
    for(int i=0; i<=1; ++i)
        for(int j=0; j<=1; ++j)
        {
            c[i][j]=0;
            for(int k=0; k<=1; ++k)
                c[i][j] += a[i][k]*b[k][j] %mod;
            c[i][j]%=mod;
        }
    for(int i=0; i<=1; ++i)
        for(int j=0; j<=1; ++j) a[i][j]=c[i][j];
}

ll res[3][3], m[3][3];
void pw(ll a,ll b,ll n)
{
    res[0][0]=res[1][1]=1;
    m[0][0]=a; m[0][1]=b; m[1][1]=1;

    while(n)
    {
        if(n&1ll) mul(res,m);
        mul(m,m);
        n>>=1ll;
    }
}

int main(void)
{
    ll a,b,n,x;
    cin>>a>>b>>n>>x;

    pw(a,b,n);
    cout<< (x*res[0][0] + res[0][1]) %mod;
    return 0;
}

E. Another Sith Tournament

n很小,所以考虑状压

转移时就枚举一个没参加过的人$j$,分别考虑$i$打败$j$和$j$打败$i$的情况 计算答案时,枚举初始的两个人即可 ```cpp #include<cstdio> #include<algorithm> typedef long double db; const int MAXN = 18 + 5; const int MAXS = (1<<18) + 5; using namespace std; inline void chk_max(db &a,db b){ if(a<b) a=b;} int n,maxs; db a[MAXN][MAXN]; db dp[MAXS][MAXN]; db gao(int s,int i) { if(dp[s][i]>=-0.5) return dp[s][i]; if(s==maxs) return dp[s][i]=!i; db &res=dp[s][i]; for(int j=0; j<n; ++j) if(((s>>j)&1)==0) chk_max(res, a[i][j]*gao(s|(1<<j), i)+a[j][i]*gao(s|(1<<j), j)); return res; } int main(void) { scanf("%d",&n); if(n==1) { printf("1"); return 0; } for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) { double p; scanf("%lf",&p); a[i][j]=p; } maxs=(1<<n)-1; for(int s=0; s<=maxs; ++s) for(int i=0; i<n; ++i) dp[s][i]=-1; db ans=0; for(int i=0; i<n; ++i) for(int j=0; j<i; ++j) { int s=(1<<i)|(1<<j); chk_max(ans, a[i][j]*gao(s,i)+a[j][i]*gao(s,j)); } printf("%.20lf",(double)ans); return 0; } ```