Educational Codeforces Round 88 (Rated for Div. 2) 参赛人数19992
[codeforces 1359E] Modular Stability 快速幂+乘法逆元+组合数计算+找规律
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1359/problem/E
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
E - Modular Stability | GNU C++17 | Accepted | 46 ms | 8100 KB |
样例1是突破口
7 3
16
1,2,3,4,5,6,7
上面7个数中,选3个数,无论怎么变顺序,模后的结果都不会变。
组合如下
1,2,3
1,2,4
1,2,5
1,2,6
1,2,7
1,3,4
1,3,5
1,3,6
1,3,7
1,4,5
1,4,6
1,4,7
1,5,6
1,5,7
1,6,7
2,4,6
以1,3,6为例
17%6%3%1=0,17%6%1%3=0,17%3%6%1=0,17%3%1%6=0,17%1%3%6=0,17%1%3%6=0
16%6%3%1=0,16%6%1%3=0,16%3%6%1=0,16%3%1%6=0,16%1%3%6=0,16%1%3%6=0
以2,4,6为例
17%2%4%6=1,17%2%6%4=1,17%4%2%6=1,17%4%6%2=1,17%6%2%4=1,17%6%4%2=1
16%2%4%6=0,16%2%6%4=0,16%4%2%6=0,16%4%6%2=0,16%6%2%4=9,16%6%4%2=0
猜想如下
10 3
1,2,3,4,5,6,7,8,9,10
1.首个数是1,剩下的2个数在(2,3,4,5,6,7,8,9,10)里挑
2.首个数是2,剩下的2个数在(4,6,8,10)里挑
3.首个数是3,剩下的2个数在(6,9)里挑
验证第3种情况
3,6,9
17%3%6%9=2,17%3%9%6=2,17%6%3%9=2,17%6%9%3=2,17%9%3%6=2,17%9%6%3=2
16%3%6%9=1,16%3%9%6=1,16%6%3%9=1,16%6%9%3=1,16%9%3%6=1,16%9%6%3=1
猜测规律如下
for(i=1;i<=n;i++)ans=(ans+C(n/i-1,k-1))%mod;
样例3可以作为验证规律正确与否的测试数据
Input:
1337 42
Output:
95147305
AC代码如下
#include <stdio.h>
#define maxn 500010
#define mod 998244353
#define LL long long
LL fact[maxn],inv[maxn];
int n,k;
LL quick_pow(LL a,LL b){//快速幂
LL ret=1;
while(b){
if(b&1)ret=ret*a%mod;
a=a*a%mod;
b>>=1;
}
return ret%mod;
}
void init(){
int i;
scanf("%d%d",&n,&k);
fact[0]=1;
for(i=1;i<=n;i++)fact[i]=fact[i-1]*i%mod;//阶乘
inv[n]=quick_pow(fact[n],mod-2);//阶乘的乘法逆元
for(i=n-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
}
LL C(int n,int m){//组合数计算
if(n<m)return 0;
return fact[n]*inv[n-m]%mod*inv[m]%mod;
}
void solve(){
int i;
LL ans=0;
for(i=1;i<=n;i++)ans=(ans+C(n/i-1,k-1))%mod;
printf("%lld\n",ans);
}
int main(){
init();
solve();
return 0;
}