NC231373. Chocolate Counting
描述
Let be a prime number and be a positive integer. There are a total of bars of chocolate at the shop, the -th of which sells at the price of coins (). A total of students are choosing presents for the coming Teachers' Day together. They decide to buy exactly different bars of chocolate, and they hope that the number of coins they need to pay is divisible by , so that they can divide the bill equally. Please count the number of ways for the students to choose some chocolates, such that the total price is divisible by . Two ways of selecting are considered different, if there exists a bar chosen in one way but not in the other. As the answer may be too large, you just need to print it modulo .
给定质数 和正整数 。商店里有价格为 的巧克力各一盒。小朋友们想要从中选出 盒,使得价格的总和是 的倍数。请计算满足上述要求的选法数量。两种选法视作不同当且仅当存在一盒巧克力在一种选法被选而另一种选法中没有。由于答案可能很大,你只需要输出它对 取模后的值即可。
输入描述
The input consists of multiple test cases. The first line contains a single integer , denoting the number of test cases.
The -th of the next lines contains two integers , describing the situation for the -th test case. It is guaranteed that is a prime number.
输入包含多组测试数据。第一行一个正整数 表示测试数据的组数。
接下来 行每行两个正整数 ,即为一组测试数据中 的取值。保证 是质数。
输出描述
Print a single integer for each test case, denoting the answer modulo .
对每组数据输出一行,表示答案对 取模后的值。
示例1
输入:
1 3 2
输出:
8
说明:
In the example, there are a total of students and bars of chocolate, with the price of each. There are a total of ways to select chocolates to make their price a multiple of , listed below:
样例输入中 ,我们要在价格为 的巧克力中选出 盒使得价格和为 的倍数。一共有 种不同的选法,列举如下:
C++ 解法, 执行用时: 580ms, 内存消耗: 78644K, 提交时间: 2021-12-05 13:42:36
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=10000007,P=998244353; ll T,n,k,ans,inv[N]; ll C(ll x,ll y){ ll s=1; for(int i=1;i<=y;i++)s=s*inv[i]%P; for(int i=0;i<y;i++)s=(x-i)%P*s%P; return s; } int main(){ inv[1]=1; for(int i=2;i<N;i++)inv[i]=(P-P/i)*inv[P%i]%P; for(cin>>T;T;T--){ cin>>n>>k; cout<<((C(n*k,n)+P-k)*inv[n]+k)%P<<'\n'; } return 0; }