题目: 求 (n-1)!mod(n)的值。
很显然当 n 是合数时 结果为0(当然这里有个特例4)
当 n 为素数时, 直接用 威尔逊定理 结果为n-1.
#include#include #include #include #define LL long long using namespace std; bool is_prime(LL n) { for( int i= 2; i*i<=n; i++ ) { if(n%i== 0 ) return false ; } return true ; } int main() { int T; LL n; scanf( " %d ", & T); while(T-- ) { scanf( " %lld ", & n); if(n== 4 ) printf( " 2\n " ); else if (is_prime(n)) { printf( " %lld\n ", n- 1 ); } else printf( " 0\n " ); } return 0 ; }
转载于:https://www.cnblogs.com/acm1314/p/4769313.html
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229499.html原文链接:https://javaforall.net
