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;
int vis[100010];
int prime[100010], c;

void sieve(int n)
{
	int m = sqrt(n+0.5);
	memset(vis, 0, sizeof(vis));
	vis[0] = vis[1] = 1;
	for(int i = 2; i <= m; i++)
		if(!vis[i])
			for(int j = i*i; j <= n; j += i)
				vis[j] = 1;
}

int get_primes(int n)
{
	sieve(n);
	int c = 0;
	for(int i = 2; i <= n; i++)
		if(!vis[i])
			prime[c++] = i;
	return c;
}
LL pow(LL a, LL b, LL n)
{
	LL ans = 1;
	while(b)
	{
		if(b&1)
		{
			ans *= a;
			ans %= n;
		}
		b >>= 1;
		a *= a;
		a %= n;
	}
	return ans;
}
LL work(LL x, LL y)
{
	LL ans = 0;
	while(x)
	{
		ans += x/y;
		x /= y;
	}
	return ans;
}
LL cm(LL n, LL m, LL p)
{
	LL ans = 1;
	for(int i = 0; prime[i] <= n && i < c; i++)
	{
		LL x = work(n, prime[i]);
		LL y = work(n-m, prime[i]);
		LL z = work(m, prime[i]);
		x -= y+z;
		ans *= pow(prime[i], x, p);
		ans %= p;
	}
	return ans;
}
LL cal(LL n, LL k, LL l, LL m)
{
	LL ans = 1;
	ans = ans * pow(k, l, n) % n;
	ans = ans * pow(k-1, m, n) % n;
	ans = ans * cm(l, m, n) % n;
	return ans;
}
int main()
{
	c = get_primes(100000);
	int T;
	int cas = 1;
	scanf("%d", &T);
	while(T--)
	{
		LL n, k, l, m;
		scanf("%lld %lld %lld %lld", &n, &k, &l, &m);
		if(m == 0)
		{
			printf("Case %d: %lld\n", cas++, pow(k, l, n)+1);
		}
		else if(k == 1)
			printf("Case %d: 1\n", cas++);
		else
			printf("Case %d: %lld\n", cas++, cal(2*n, k, l, m)/2+1);

	}
	return 0;
}
时间: 2024-08-27 07:15:31

Light OJ 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 1268 Unlucky Strings 矩阵快速幂+KMP

题目来源:Light OJ 1268 Unlucky Strings 题意:给你一些可以用的字符 然后求组成不包含给定字符串的方案数 思路:矩阵经典问题 从i走k步路到达j的方案数 可以用矩阵快速幂求解 对于求长度为n的字符的方案数 就是走n步路 求走法 可以用KMP求出走一步 从前i个字符到前j个字符的方案数 这点有点不好理解 想一想 #include <cstdio> #include <cstring> #include <algorithm> #include

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 gam

LIGHT OJ 1278 Sum of Consecutive Integers(奇因子的个数)

题目链接:传送门 题意: 将给定的n分成 连续的数的和,至少有两个数,看能有多少种方案. 分析: a + (a + 1) + (a + 2) + ... +(a + k - 1) = n; ===> (2*a + k - 1) * k = 2*n; ===> (2*a - 1)*k = 2*n - k*k; ===> 2*a - 1 = 2*n/k - k; 等式的左边为奇数,那么右边也必须为奇数,则k必须为n的奇约数 因此将n素因子分解就可以了 代码如下: #include <i

Light OJ 1102 Problem Makes Problem 组合数

#include <iostream> #include <string> #include <map> #include <vector> #include<algorithm> using namespace std; typedef pair<string,int>PAIR; bool cmp_by_value(const PAIR& p,const PAIR &a) { return p.second<a

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

vijos - P1739计算系数 (多项式计算 + 杨辉三角形 + 高速幂)

P1739计算系数 Accepted 标签:NOIP提高组2011[显示标签] 描写叙述 给定一个多项式(ax + by)^k,请求出多项式展开后x^n * y^m项的系数. 格式 输入格式 共一行,包括5个整数,分别为a,b,k.n,m,每两个整数之间用一个空格隔开. 输出格式 输出共1行,包括一个整数,表示所求的系数.这个系数可能非常大.输出对10007取模后的结果. 例子1 例子输入1[复制] 1 1 3 1 2 例子输出1[复制] 3 限制 1s 提示 对于30%的数据,有0 ≤ k ≤

HDU4869:Turn the pokers(费马小定理+高速幂)

Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, tending to play poker. But she hated the traditional gameplay. She wants to change. She puts these pokers face down,

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g