3 UVA 10852 Less Prime

Source: Internet
Author: User

First Sieve Prime,

Then enumerate the prime numbers in the topic range, the answer is to find the smallest ans=n-n/i*i, where I is the prime number of the enumeration



#include <cstdio> #include <algorithm> #include <cstring>using namespace Std;bool pri[10010];void Init () {    int i,j;    memset (pri,1,sizeof pri);    pri[0]=pri[1]=0;    for (i=2;i<=10005;i++)    {        if (!pri[i]) continue;        for (j=i+i;j<=10005;j+=i)            pri[j]=0;    }} int main () {    init ();    int t,i,n,ans,x;    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        Ans=0,x=n;        for (i=2;i<=n;i++)        {            if (Pri[i])            {                if (ans<n-n/i*i) ans=n-n/i*i,x=i;            }        }        printf ("%d\n", x);    }    return 0;}


3 UVA 10852 Less Prime

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.