CTU open contest 2019 A. beer bars (number of combinations)

Posted by slands10 on Sat, 22 Feb 2020 17:23:12 +0100


Title:

Give four integers: A, B, K, CA, B, K, CA, B, K, C, A,B,CA,B,CA,B,C are all single digits greater than 0. Ask how many numbers of CCC are among all KKK digits only composed of AAA or BBB (each digit of KKK digits is AAA or BBB).

There are three situations:

  1. When a=b=ca=b=ca=b=c, it must be a case of composition. The number is kkk.
  2. When a!=ca!=ca!=c and b!=cb!=cb!=c, it must be 000.
  3. When a = CA = CA = C | B = CB = CB = C, the total combination is 2k2 ^ K2 K, then the number of ccc is 2K * k2\frac{2^k*k}{2}22k * k, here we use the fast power to find 2K − 12^{k-1}2k − 1

This question card I fast power, before used half a year's template used on the wrong.

AC Code:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

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);
}
///Fast power m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}
// Fast power inverse element
int Fermat(int a, int p) //Fermat finds the inverse element of a about b
{
    return qpow(a, p - 2, p);
}

///Extended Euclid
ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll g = exgcd(b, a % b, x, y);
    ll t = x;
    x = y;
    y = t - a / b * y;
    return g;
}
const int N = 1e6 + 10;
int n, m;
int a, b, k, c;

int main()
{
    sdd(a, b);
    sdd(k, c);
    ll ans = 1;
    if (a == c && b == c)
    {
        pd(k);
    }
    else if (c != a && c != b)
    {
        puts("0");
    }
    else
    {
        ans = qpow(2, k - 1, MOD) % MOD;
        ans = ans * 1ll * k % MOD;
        pld(ans);
    }
    return 0;
}
669 original articles published, 407 praised, 210000 visitors+
His message board follow