URAL 1907. Coffee and Buns (number theory deduction + repulsion principle)

Source: Internet
Author: User

1907. Coffee and Bunstime limit:1.0 second
Memory limit:64 MB
Planet Ataraxia is known for its education centers. The people who is expected to take high social positions in future is brought up in conditions of continuous training an D supervision from the early age. Each of the education center is a small and all the necessary utilities. During the construction of a center, a square area is chosen which are divided into equal sections each sized 100x100 met ERs. In each of the these sections they build one building, which would become residential or academic. The outer perimeter of the center is fenced. After a successful military operation in the Andromeda Nebula the active colonization of habitable planets have started. The need for people able to take command and leads people to the new worlds have increased. Therefore, the new education centers should be built on Ataraxia. Discussion about the details of the project in the local administration are already underway for many days. During this time it is decided that the first center would consist OF&NBsp a2Sections and that the second one would consist of no more than N2Sections. The situation is complicated because, according to requirements of the antimonopoly legislation, construction works must b E performed by at least-different companies, each of the them must build an equal number of buildings and an equal number of 100-meters segments of the fence. You is responsible for resupplying the administration office. You understand this while they is discussing the pros and cons of each possible size of the second center a lot of buns a nd coffee would be consumed, and it's time to buy them. So you ' d like to know how many different sizes of the second center would meet the requirements of antimonopoly legislation And, therefore, 'll be fully considered by the administration. Inputthe only line contains integers aand N(1≤ a≤10 A; 1≤ N≤10 -). Outputoutput an amount of different sizes of the second center meeting the requirements of antimonopoly. Sample
input output
3 6 
< Pre class= "intable" style= "Font-family:monospace, ' monospace for Firefox '; Font-size:0.81em; margin-top:0px; margin-bottom:0px ">4
Notesin This example it's possible to build the second center sized 3x3 or 6x6, delegating the construction to three Different companies, or to build it sized 1x1 or 5x5, delegating the construction to the different companies. problem Author:Ivan Burmistrov
problem Source:Ural Championship 2012
Tags:None() difficulty:985 Printable version Submit solution Discussion (0)
All submissions (654) all accepted submissions (223) Solutions Rating (123)

Approximate test instructions:

Enter a n to determine the number of GCD ((A * a + b * b), 4 * A + 4 * b) to be met from B = 1 ~ N and 1.

A < 1e12, n < 1e18

If the parity of a B is the same, it is obviously satisfied. GCD is a multiple of 2.

If a B parity is different,

Start derivation:

Because A * a + b * B is an odd number,

So

GCD (A * a + b * B, 4 * A + 4 * b) > 1 equivalent to

GCD (A * a + b * B, a + B) > 1 equivalent to

According to the divisible nature, if present E > 1, meet E | (A + B), then E | (A + B) ^ 2 and vice versa

is equivalent to

GCD (A * a + b * B, A * a + b * b + 2 * A * b) > 1 equivalent to

GCD (2 * A * a + 2 * b * B, A * a + b * b + 2 * A * b) > 1 equivalent to

GCD (A * a + b * b-2 * A * b, a * a + b * b + 2 * A * b) > 1 equivalent to

GCD (A-B) > 1 equivalent to

GCD (2 * A, A + b) > 1

Because A+b is odd.

Equivalent to

GCD (A, a +b) > 1 is equivalent to

GCD (A, B) > 1

Deduction End

The following is a decomposition of the factorization, find the n number of different parity and meet gcd (A, B) > 1 number

The number of the last plus parity is the answer.


Therefore, the number of 1~MAXN and A,GCD > 1 is required, that is, to find the number of 1~MAXN and a single num, to use the principle of repulsion:

ll No_coprime (ll num,ll maxn) {//1~MAXN with num-non-reciprocal number        ll ans = 0;        vector<ll> FAC;        for (ll i = 2; i*i <= num; i++) {//Decomposition factor                if (num% i = = 0) {                        fac.push_back (i);                        while (num%i = = 0) num/= i;                }        }        if (num! = 1) fac.push_back (num);        int sz = SZ (FAC);        for (ll mask = 1; mask < (1LL<<SZ); mask++) {//Tolerance process, complexity 2^ due to several number                ll c = 0;                ll tmp = 1;                Rep (I,sz) if ((1ll<<i) &mask) {                        C + +;                        TMP *= fac[i];                }                if (c&1) ans + = maxn/tmp;                else ans-= maxn/tmp;        }        return ans;}



AC code:

Accepted 41215 g++ 4.9 c++11 2092//#pragma comment (linker, "/stack:1024000000,1024000000") #include <iostream># Include <cstring> #include <cmath> #include <queue> #include <stack> #include <map># Include <set> #include <string> #include <vector> #include <cstdio> #include <ctime># Include <bitset> #include <algorithm> #define SZ (x) ((int) (x). Size ()) #define ALL (v) (v). Begin (), (v). End () # Define foreach (I, v) for (__typeof ((v). Begin ()) I = (v). Begin (); I! = (v). End (); + + i) #define Reveach (i, v) for (__typeof ( (v). Rbegin ()) i = (v). Rbegin (); I! = (v). rend (); + + i) #define REP (i,n) for (int i=1; I<=int (n), i++) #define REP (i,n) for (int i=0; i<int (n); i++) using namespace Std;typedef long long ll; #define X first#define Y secondtypedef pair<ll,ll> pii;template <class t>inline bool  RD (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | c> ' 9 ')) c = GetcHar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void PT (T x) {if (x < 0) {Putchar ('-'); x =-X;} if (x > 9) pt (X/10);p Utchar (x% 10 + ' 0 ');}        ll A,n;ll No_coprime (ll num,ll maxn) {ll ans = 0;        vector<ll> FAC;                        for (ll i = 2; i*i <= num; i++) {//Decomposition factor if (num% i = = 0) {fac.push_back (i);                while (num%i = = 0) num/= i;        }} if (num! = 1) fac.push_back (num);        int sz = SZ (FAC);                for (ll mask = 1; mask < (1LL&LT;&LT;SZ); mask++) {//Tolerance process, complexity 2^ due to several number ll C = 0;                ll tmp = 1;                        Rep (I,sz) if ((1ll<<i) &mask) {C + +;                TMP *= Fac[i];                } if (c&1) ans + = maxn/tmp;  else ans-= maxn/tmp;      } return ans;                int main () {while (cin>>a>>n) {ll ans = 0;                if (a&1) ans + = (n+1)/2, n/= 2;                Ans + = No_coprime (a,n);        printf ("%lld\n", ans); }}


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

URAL 1907. Coffee and Buns (number theory deduction + repulsion principle)

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.