1501131824-Blue Bridge cup-algorithm training Torry Puzzle (basic type)

Source: Internet
Author: User

Algorithm Training Torry Puzzle (basic type) time limit: 1.0s memory limit: 512.0MBProblem description Torry from childhood love mathematics. One day, the teacher told him, like 2, 3, 5, 7 ... Such numbers are called prime numbers. Torry suddenly thought of a problem, the first 10, 100, 1000, 10000 ... What is the product of a prime number? He told the teacher the question. The teacher was stunned and couldn't answer it. So Torry turned to the programmer, and asked you to calculate the product of the first n prime numbers. However, considering that you are only in touch with programming soon, Torry as long as you figure out the value of 50000 on the modulo. The input format contains only a positive integer n, where n<=100000. The output format outputs a row, which is the value of the product modulo 50000 of the first n prime numbers. Example input 1 Sample output 2 Thinking of solving problemsbecause I do not know what the 100,000th prime number is, so we can first use the larger n when the end of the table to 0--n direct prime numbers, and then starting from 0 to judge if the prime number is multiplied and j + +, you can enter the 100000来 to determine which numbers to cut off, and then modify the program's N. Note that the answer requires a 64-bit int. This problem also examines the congruence theorem. C code
#include <stdio.h> #include <string.h>int a[11000000],b[110000];int main () {int n;int i,j,k;__int64 answer; scanf ("%d", &n), memset (A,0,sizeof (a)), A[0]=a[1]=1;for (i=0;i<10000000;i++) {if (A[i])    continue;for (j=i+ i;j<10000000;j+=i)    a[j]=1;} Answer=1;for (i=0,j=0;i<10000000&&j<n;i++)//Because the output nth and J is starting from 0, so use < is good     if (a[i]==0)    {        b[j]=i;        Answer= ((answer%50000) * (b[j]%50000))%50000;        Note that when an=49999,b[j]=49999, the product exceeds the int type.         j + +;    } printf ("%d", I);   Since I do not know what the 100,000th prime number is, I test the input 100000 output I to when.     printf ("%i64d\n", answer); return 0;}

Java code
Package Torry Confusion; import java.util.scanner;public class main{public static void Main (string[] args) {Scanner input=new Scanner (system.in); int[] A=new int[1100000];int[] b=new int[110000];int n=input.nextint (); int I,j,k;long answer;a[0]= A[1]=1;for (i=0;i<1000000;i++) {if (a[i]==1) continue;for (j=i+i;j<1000000;j+=i) a[j]=1;} Answer=1;for (i=0,j=0;i<1000000&&j<n;i++) if (a[i]==0) {b[j]=i;answer= ((answer%50000) * (b[j]%50000))% 50000;j++;} System.out.println (answer);}}


1501131824-Blue Bridge cup-algorithm training Torry Puzzle (basic type)

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.