Ultraviolet (a) 1434-YAPTCHA (number theory)

Source: Internet
Author: User

Ultraviolet A 1434-YAPTCHA

Question Link

Question: Find the answer to the formula in the question

Ideas:
When 3?K+ 7 when it is not a prime number, then (3?K+ 6 )! (Because we can certainly find two factors to multiply)
Therefore, the original formula is 0.

When 3?K+ When 7 is a prime number, according to Wilson's theorem, (3?K+ 6 )! + 1) % (3?K+ 7) = 0, so the original formula can be converted to [x-(x-1)] = 1

Therefore, if the problem is converted to determining whether 3 * k + 7 is a prime number, it is easy to prepare the prime number table and then pre-process the answer.

Code:

# Include

 
  
# Include

  
   
Const long N = 1000005; bool ispri [N * 3]; int ans [N], t, n; int main () {for (long I = 2; I <N * 3; I ++) {if (ispri [I]) continue; for (long j = I * I; j <N * 3; j + = I) ispri [j] = true;} for (int I = 2; I <N; I ++) ans [I] = ans [I-1] + (! Ispri [3 * I + 7]); scanf ("% d", & t); while (t --) {scanf ("% d", & n ); printf ("% d \ n", ans [n]);} return 0 ;}

  

 


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.