LightOJ - 1318 - Strange Game(组合数)

链接:

https://vjudge.net/problem/LightOJ-1318

题意:

In a country named "Ajob Desh", people play a game called "Ajob Game" (or strange game). This game is actually a game of words. The rules for the game are as follows:

It‘s an N player game and players are numbered from 1 to N. And the players alternate turns in a circular way. Player 1 starts first. The next turn is for player 2, then player 3 and so on. After the turn for the Nth player, player 1 gets his turn again and the same procedure is continued.
In each turn a player has to propose a pair of words. Each of the words should have length L, and the words should differ in exactly M positions. As their language has K alphabetical symbols, a word is a collection of symbols from these K alphabets.
The pair of words proposed by a player should differ in exactly M positions, it means that there should be exactly M positions where the two words have different symbols, and in other positions they have same symbols. For example, ‘abc‘ and ‘abd‘ differ in exactly 1 position, ‘abc‘ and ‘aca‘ differ in exactly 2 positions, ‘abc‘ and ‘cab‘ differ in exactly 3 positions.
In each turn a player has to propose a new pair of words. Two pairs are different if at least one word is different. Note that here pair refers to unordered pair. Let A, B, C be three different words, then (A, B) and (B, A) are same, but (A, C) and (A, B) are different. For example, if a player already proposed {abc, def}, then none can propose {abc, def} or {def, abc}. But a player can propose {abc, fed} or {abc, abc} or {pqc, abc} etc.
If a player fails to propose a new pair of words, he is treated as the loser of the game. And the game ends.
Let N = 2, K = 2, L = 2, M = 1 and the alphabet is {ab}. All the words of length 2 are: {aa, ab, ba, bb}. Player 1 chooses pair {aa, ab} (differs in 1 position as M = 1) then player 2 chooses pair {ab, bb}. After that player 1 chooses {aa, ba} then player 2 chooses {bb, ba}. And then there is no pair left for player 1, and so, player 1 will lose.

Now this game is played by N players who know this game very well thus they play optimally. You are given N, K, L and M; you have to find the loosing player.

思路:

一对字符串,一边是,\(k^l\)种,同时另一边需要有m个不同,则是\((k-1)^m\)
共有\(C_l^m*(k-1)^m*k^l\)
同时有重复,要除2,考虑2和n不一定互质,当k为偶数时用k/2,否则用(k-1)/2

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

LL n, k, l, m;
int pos;
int Pri[MAXN], Isp[MAXN], Cnt[MAXN];

void Init()
{
    pos = 0;
    for (int i = 2;i < MAXN;i++)
    {
        if (Isp[i] == 0)
            Pri[++pos] = i;
        for (int j = 1;j <= pos && 1LL*i*Pri[j] < MAXN;j++)
        {
            Isp[i*Pri[j]] = 1;
            if (i%Pri[j] == 0)
                break;
        }
    }
}

void Upd(LL x, int sta)
{
    for (int i = 1;i <= pos;i++)
    {
        LL tmp = x;
        while(tmp)
        {
            Cnt[i] += sta*(tmp/Pri[i]);
            tmp /= Pri[i];
        }
    }
}

LL PowMod(LL a, LL b, LL p)
{
    LL res = 1;
    while(b)
    {
        if (b&1)
            res = res*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return res;
}

LL C(LL n, LL m, LL p)
{
    memset(Cnt, 0, sizeof(Cnt));
    Upd(l, 1), Upd(m, -1), Upd(l-m, -1);
    LL ans = 1;
    for (int i = 1;i <= pos;i++)
        ans = ans*PowMod(Pri[i], Cnt[i], p)%p;
    return ans;
}

int main()
{
    // freopen("test.in", "r", stdin);
    Init();
    int t, cas = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cas);
        scanf("%lld%lld%lld%lld", &n, &k, &l, &m);
        LL ans;
        if (m != 0)
        {
            if (k&1)
                ans = C(l, m, n)*PowMod(k, l, n)%n*PowMod(k-1, m-1, n)%n*(k/2)%n;
            else
                ans = C(l, m, n)*PowMod(k, l-1, n)%n*PowMod(k-1, m, n)%n*(k/2)%n;
        }
        else
            ans = PowMod(k, l, n);
        printf(" %lld\n", ans+1);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12019437.html

时间: 2024-10-13 15:38:50

LightOJ - 1318 - Strange Game(组合数)的相关文章

Light OJ 1318 Strange Game 组合数+快速幂+分解因子

长度为l的用k种字符组成的字符串有k^l中 其中m个字符要不相同 那就是k^l*C(l, m)*(k-1)^m 有重复 要除以2 但是你mod n了 不能直接除 n不一定是素数 所以不能乘以逆元 所以我都mod 2倍的n 最后的结果再除以2 特判l = 1 和 m = 0的情况 #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL;

Light OJ 1318 Strange Game 组合数+高速幂+分解因子

长度为l的用k种字符组成的字符串有k^l中 当中m个字符要不同样 那就是k^l*C(l, m)*(k-1)^m 有反复 要除以2 可是你mod n了 不能直接除 n不一定是素数 所以不能乘以逆元 所以我都mod 2倍的n 最后的结果再除以2 特判l = 1 和 m = 0的情况 #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL;

LightOJ - 1067 - Combinations(组合数)

链接: https://vjudge.net/problem/LightOJ-1067 题意: Given n different objects, you want to take k of them. How many ways to can do it? For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways. Take 1, 2 Take 1, 3 Take 1, 4

LightOJ - 1005 - Rooks(组合数)

链接: https://vjudge.net/problem/LightOJ-1005 题意: A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one i

lightoj 1095 - Arrange the Numbers(dp+组合数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 dp[i]=(i-1)*dp[i-2](表示新加的那个数放到i-1中的某一个位置然后那个被放位置的数放在i这个位置就是i-2的错排)+(i-1)*dp[i-1](表示新加的那个数放到i-1中的某一个位置然后用那个位置被占的数代替i这个位置的数就是i-1的错排) #include <iostream

lightoj 1060 - nth Permutation(组合数+贪心)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1060 题解:如果是不重复数的这些操作可以用康托展开的逆来求,如果是有重复数字出现康托展开的逆就要稍微变一下.要除去自身个数的组合数具体看一代码,暴力就行 #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll;

LightOJ - 1246 Colorful Board(DP+组合数)

http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间不能涂相同的颜色,每个格子都必须有颜色,问可行的方案数. 分析 经一波分析,根据曼哈顿距离为奇数这一信息,可以将棋盘分为两部分,也就是相邻格子不能有相同颜色.一种颜色只能在一个部分中出现.现在考虑对一个部分的格子操作, dp[i][j]表示i个格子选择用了j种颜色的方案数,于是可以得到这样的递推式:

LightOJ 1067 组合数取模

Description Given n different objects, you want to take k of them. How many ways to can do it? For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways. Take 1, 2 Take 1, 3 Take 1, 4 Take 2, 3 Take 2, 4 Take 3, 4 Input

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven