Bestcoder Round #51 (Div.2) Zball in Tina town

Source: Internet
Author: User

Wilson's theorem: When and only if p is a prime number: (p-1)!≡p-1 (mod p) This is a sufficient condition

This problem is done with Wilson's theorem, but don't know the theorem, it doesn't matter, can make a table,

Summary: Some problems of number theory even if you do not know a certain knowledge point, but also try to lay the table, see if you can find the law

#include <cstdio>int main () {for    (int i=1;i<=13;i++)    {        int sum=1;        for (int j=1;j<i;j++)            sum*=j;        printf ("%d\n", sum%i);    }    return 0;}

Run, you can observe, composite results are 0, prime number results are n-1, there is an exception, that is 4, it also output 2;


Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5391



#include <cstdio> #include <cmath>int main () {    int t;    scanf ("%d", &t);    while (t--)    {        int n;        scanf ("%d", &n);        if (n==4) {printf ("2\n"); continue;}        if (n==1) {printf ("1\n"); continue;}        int flag=1;        int M=floor (sqrt (n*1.0) +0.5);//Note that this long expression cannot be placed in the for-judging condition and will time out for        (int i=2;i<=m;i++)        {            if (n%i==0)            {                flag=0;break;            }        }        if (flag) printf ("%d\n", n-1);        else printf ("0\n");    }    return 0;}


Copyright NOTICE: Reprint must please indicate the source, thank you

Bestcoder Round #51 (Div.2) Zball in Tina town

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.