长度为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;
}

Light OJ 1318 Strange Game 组合数+高速幂+分解因子的更多相关文章

  1. LightOJ - 1318 - Strange Game(组合数)

    链接: https://vjudge.net/problem/LightOJ-1318 题意: In a country named "Ajob Desh", people pla ...

  2. Light OJ 1060 - nth Permutation(组合数)

    题目大意: 给你一个字符串,问这个字符串按照特定顺序排列之后,第n个字符串是哪个? 题目分析: 首先我们要会求解总个数.也就是共有len个字符,每个字符有ki个,那么总组合方式是多少种? 总组合方式就 ...

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

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

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

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

  5. [BZOJ3209]花神的数论题 组合数+快速幂

    3209: 花神的数论题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2498  Solved: 1129[Submit][Status][Disc ...

  6. poj 1845(等比数列前n项和及高速幂)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13959   Accepted: 3433 Descripti ...

  7. HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  8. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

  9. HDOJ--4869--Turn the pokers【组合数学+高速幂】

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 题意:有m张扑克.開始时所有正面朝下.你能够翻n次牌,每次能够翻xi张.翻拍规则就是正面朝下变背面朝 ...

随机推荐

  1. stack smashing detect错误修正

    运行./a.out程序时候出现如下: *** stack smashing detected ***: ./a.out terminated段错误 (核心已转储) 一般这个错误是由于堆栈错误,很可能是 ...

  2. BZOJ 1898: [Zjoi2004]Swamp 沼泽鳄鱼( dp + 矩阵快速幂 )

    ----------------------------------------------------------------------- #include<cstdio> #incl ...

  3. MYSql和PHP计算数据性能

    MYSQL不是有很多内部计算函数吗? 比如我要计算一列数据的平均值,,那我是直接用MYSQL里面的函数在SQL语句中计算 快 :还是用SQL把数据取出来了,用PHP计算快呢?(SQL语句PHP语句都用 ...

  4. Yii框架中的CURD操作

    <?php $Admin = new Admin(); //查找多条记录,返回二维数组 $Admin->findAll(); $Admin->findAll("id = 2 ...

  5. ubuntu 10.04 下 samba 服务的配置

    1. 安装 $ sudo apt-get install samba $ sudo apt-get install samba-common $ sudo apt-get install samb . ...

  6. 高级UNIX环境编程2

    perror("error:") ;  strerror 日历时间:time_t (1970.1.1开始的秒数)     struct timeval (秒数和微秒)  struc ...

  7. (IOS)数据持久化

    1.属性列表序列化 2.模型对象归档 3.嵌入式SQLite3 4.Core Data 5.应用程序设置 6.UIDocument管理文档存储 7.iCloud Demo界面: 1.属性列表序列化 即 ...

  8. asp.net从客户端检测到有潜在危险的Request.Form 值

    asp.net开发中,经常遇到“从客户端检测到有潜在危险的Request.Form 值”错误提示,很多人给出的解决方案是: 1.web.config文档<system.web>后面加入这一 ...

  9. Air Raid(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 Descript ...

  10. iOS 拍照保存到相册

    之前看了一些开源的代码,里面有一个功能,就是将图片下载到相册,仔细看了其中的代码,只有很简单的一句话,并且保存过后,还可以判断是否保存成功. 如下代码所示, 点击按钮,将self.imageView上 ...