hdu5391 Zball in Tina town (prime Sieve method)

Source: Internet
Author: User

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

The main topic: a ball initial volume of 1, daily become larger, the first day to become larger 1 time times, the second day to become larger twice times, the nth day to become large n times. Ask when the first N-1 day, the volume becomes how much. Note the answer to n modulo.

Idea: The topic meaning has been engaged for a long time, in fact is the first day is 1, the next day is 1*2, the third day is 1*2*3, that is, when the nth day is n!. So the answer is (n-1)! N

The topic inside n is 10^9, which can not directly get factorial. At this point, you can think about it and find out the rules.

After playing the table found that if it is prime, the answer is n-1, otherwise it is 0. Of course, 4 here is an exception.

The question then turns into whether N is a prime number. It's easy to find the primes. The simplest method: for a prime number n, see within his range of 2~SQRT (n), can be divisible by him.

Note This program, I just started using __int64 to define variables, and the result is tle. Because of this situation, reported to try the mentality changed to int, on AC, ran more than 900 Ms.

#include <stdio.h> #include <string.h> #include <math.h> #define LL __int64int Main () {    int T;    int i,j,n,k;    scanf ("%d", &t);    int flag=0;    while (t--)    {        flag=0;        scanf ("%d", &n);        K=SQRT (n);        for (i=2;i<=k;i++)        {            if (n%i==0) {                flag=1;break;            }        }        if (n==4) {            printf ("2\n"); continue;        }        if (flag) printf ("0\n");        else printf ("%d\n", n-1);    }    return 0;}


In fact, at the beginning of the time, have been thinking to sift out prime numbers to do. It was later written with the linear prime sieve method. But the effect is not good, also want 800ms.

#include <stdio.h> #include <string.h> #include <math.h> #define MAX 40005int Prime[40005];bool  Isprime[40005];int tot=0;void Sushu ()//Linear prime sieve method {memset (isprime,true,sizeof (IsPrime));  memset (prime,0,sizeof (prime));  Isprime[0]=false;  Isprime[1]=false;      for (int i=2;i<=max;i++) {if (Isprime[i]) {prime[tot++]=i;          } for (int j=i;j<=tot&&i*prime[j]<=max;j++) {isprime[i*prime[j]]=false; if (! (      I%PRIME[J])) break;    }}}int Main () {Sushu ();    int T,n,k,i,j,flag;    scanf ("%d", &t);        while (t--) {flag=0;        scanf ("%d", &n);        if (n==4) {printf ("2\n"); continue;        } k=sqrt (n); for (i=0;prime[i]<=k;i++)//Use the simplest method of judging whether a number is prime, but there is optimization: in 2-k, as long as the prime numbers are evenly divisible, it is considered that he is not a prime.        Here//through the pretreatment of prime numbers, the middle of the composite sieve.            {if (n%prime[i]==0) {flag=1;break; }} if (flag==0) printf ("%d\n", n-1);        else printf ("0\n"); } return 0;}


In fact, in the thinking of the method of handling primes to determine the prime number, thought there would be a great effect, but it is not so, really is very disappointed. Then I found someone else's code for a Ms. The idea is basically consistent, that is, the method of prime sieve is somewhat different. Then worship it.

#include <iostream> #include <cstdio> #include <algorithm>using namespace std; #define MOD 258280327# Define ll long Longint prime[100000]={2};int cnt;bool is_prime (int k) {for    (int i=0;prime[i]*prime[i]<=k;i++) {        if (k%prime[i]==0) return false;    }    return true;} int main () {    int t,n;    Cnt=1;    for (int i=3;i<100000;i++) {        if (is_prime (i)) {            prime[cnt++]=i;        }    }    for (int i=1;i<=100;i++)    printf ("%d", Prime[i]);    for (int i=0;i<30;i++) cout<<prime[i]<<endl;    scanf ("%d", &t);    while (t--) {        scanf ("%d", &n);        if (n==4) {            printf ("%d\n", 2);        }        else if (Is_prime (n)) {            printf ("%d\n", n-1);        }        else printf ("0\n");    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu5391 Zball in Tina town (prime Sieve method)

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.