在第一个样例中,因为 2! = 22!=2,因此 2!!! = (((2!)!)!) = ((2!)!) = (2!) = 22!!!=(((2!)!)!)=((2!)!)=(2!)=2。2模了6324仍是2!
因此答案为2。
没想明白会很难,想明白很水...
你想,加入它大于4的话,用计算器可得三次阶乘后会直接爆掉,因此当咱们枚举的时候就会发现,你在阶乘的时候必有一次和mod相等,这样最后无论数多大最后都得0.
而后再分类讨论0,1,2,3就能够了
CODE:
#include<cstdio> #define ll long long using namespace std; int main() { ll n,mod,ans=1; scanf("%lld%lld",&n,&mod); if(n>=4) { printf("0"); return 0; } if(n==0) { printf("%lld",1%mod); return 0; } if(n==1) { printf("%lld",1%mod); return 0; } if(n==2) { printf("%lld",2%mod); return 0; } if(n==3) { for(int i=1;i<=720;i++) ans=(ll) (ans*i)%mod; printf("%lld",ans); return 0; } }