UVa 106-fermat vs. Pythagoras

Source: Internet
Author: User

Title: Find the naïve solution of a tick array of less than N (three number coprime) and find the number of digits not appearing in all the tick arrays in [1, N].

Analysis: Number theory. In this case, the solution can be directly used in "original".

x = 2st,y = S^2-t^2,z = s^2 + t^2,

Where: 1.s > t; (enumeration order)

2.s and T coprime; (naive solution)

3.s and t parity are different; (contradiction proof)

When calculating numbers that do not appear, you need to enumerate the multiples of the naïve solution.

Description: The great Euclid was ╮(╯▽╰)╭.

#include <cstring> #include <cstdio> #include <cmath>//x = 2st, y = s^2-t^2, z = s^2+t^2int gcd (int A, in T b) {return a%b?gcd (b, a%b): b;} int Visit[1000001];int Main () {int N, x, Y, Z;while (~scanf ("%d", &n)) {memset (visit, 0, sizeof (visit)); int maxt = (int)  sqrt (n+0.0), count = 0;for (int t = 1; t <= maxt; + + t) {int maxs = (int) sqrt (0.0 + n-t*t); if (Maxs > maxt) maxs =  maxt;for (int s = t+1; s <= maxs; + + s) if (s%2! = t%2 && gcd (S, t) = = 1) {Count ++;x = 2*s*t;y = S*s-t*t;z = S*s + t*t;//solves the non-naïve solution for (int k = 0; k*z <= N; + + k) Visit[x*k] = Visit[y*k] = Visit[z*k] = 1;}} int p = 0;for (int i = 1; I <= N; + + i) p + = (visit[i]==0);p rintf ("%d%d\n", count, p);}    return 0;}


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

UVa 106-fermat vs. Pythagoras

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.