Application of the principle of poj1091 tolerance and repulsion

Source: Internet
Author: User
Tags greatest common divisor

The meaning of this question is to give you n+1 number, A1 A2 ... an an+1 where 1<=ai<=m i!=n+1 an+1 = M, solution exists x1. Xn+1 make x1*a1+x2*a2+x3*a3+. +xn+1*an+1 = 1 of the number of a sequence, by the theory of the part of the knowledge we can know to be satisfied on the formula (A1, A2, A3,. AN, an+1) = 1, we can also know that there are some sequences of a greatest common divisor not equal to 1, however, we want to count the number of greatest common divisor 1, we can use the principle of tolerance, a little thought we can find (A1, A2: An+1) = 1 on the reverse side is (A1, A2, ... . an+1) = a multiple of Pi, pi is the pi in M = Pi^ai, so we define the scheme number of the AI as a multiple of pi greatest common divisor according to the repulsion principle has res = s-(a1+a2+ ... Ak) + (AI and AJ i!=j)-... The code is as follows:

#include <cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespaceStd;typedefLong LongLL;Const intMAXN =100000+Ten; LL N, M; LL PI[MAXN], NPI;BOOLVIS[MAXN];intPRIME[MAXN], num;voidShaiintN) {memset (Vis,0,sizeof(VIS)); intm = sqrt (n+0.5);  for(intI=2; i<=m; i++)if(!Vis[i]) for(intJ=i*i; j<=n; j+=i) Vis[j] =1; Num=0;  for(intI=2; i<=n; i++)if(Vis[i] = =0) Prime[num++] =i;} ll Pow (ll A, LL B)//a^b{LL res=1;  while(B >0)    {        if(B%2==1) Res= Res *A; A= A *A; B/=2; }    returnRes;}intMain () {Shai (20010);  while(cin>>n>>M) {LL TP=M; NPI=0;  for(intI=0; i<num; i++)        {            BOOLFlog =false;  while(Tp%prime[i] = =0) {TP/=Prime[i]; PI[NPI]=Prime[i]; Flog=true; }            if(flog) npi++; if(TP = =1) Break; }        if(tp!=1) pi[npi++] = TP;//It's probably not 1 after all the primes have been done here .LL res =0;  for(intI=1; i< (1&LT;&LT;NPI); i++) {LL GE=0, d=1;  for(intj=0; j<npi; J + +)                if((I&GT;&GT;J) &1) ==1) ge++, d*=Pi[j]; if(ge%2==1) Res-= POW (m/d, N); ElseRes + = POW (m/d, N); } cout<<res+pow (M, N) <<Endl; }    return 0;}

Application of the principle of poj1091 tolerance and repulsion

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.