[POI2011] Sej-strongbox

Source: Internet
Author: User
Tags gcd

Main topic:

A password box, the number is 0~n-1, which has a number of passwords, the characteristics of the password: if x is the password, y is the password, (x can be equal to Y) then (x+y)%n is also the password.

Give a n (<=10^14), a K (K<=min (250000,n)), the number of K (a[k]<n), the number of previous k-1 is not a password, the number of K is the password.

In 0~n-1, how many numbers are passwords?

Exercises

Recommendation (But the proof of conclusion two is not quite complete)

Seems to have some relationship with number theory.
And it must be a question of nature.

Conclusion 1: If x is the password, then gcd (n,x) is the password

found that x is the password, then k*x%n is the password.

So, there must be a t,c that makes T*X-N*C=GCD (n,x)

And according to the Pei genus theorem, it is not possible to make a smaller password with x than GCD (n,x),

Conclusion 2: If x, Y is a password, gcd (x, y) is the password.

According to the Pei genus theorem, p*x+q*y=gcd (x, y) has an integer solution.

If q is a negative q=-q, then p*x+ (c*n-q) *y=gcd (x, y) +c*n*y

Then there is a non-negative p,q that makes P*X+Q*Y=GCD (x, y) mod n

Conclusion 3: If x is the smallest of all passwords, then all passwords are x,2x,3x,... kx, and X is an approximate number of n.

Contrary Set X is the smallest, y is another password, if X is not an approximate number of Y, then gcd (x, y) <x, according to conclusion two, then gcd (x, y) is a smaller password. Contradiction.

Therefore, any y is a multiple of x.

Because for a password Z, according to the conclusion 1,GCD (N,Z) is also the password, so the minimum password x is gcd (n,z) of the approximate, that is, the approximate number of N.

So, if we find the X that satisfies the condition, then n/x is the answer.

We have the largest number of passwords, so x must be minimal.

Given a a[k] is the password, and X is an approximate number of n, so x must be GCD (a[k],n) of the approximate.

And, X can not be a[1~k-1], as long as it is, then X will be able to gather out AI, and AI is not a password contradiction.

As long as there is no such AI, then x must be the smallest password (Pei's theorem can be proved).

So we can enumerate the approximate gcd (a[k],n) and then sort it.

From small to large enumeration x, and then violence to verify whether it is a[i], the first x,n/x to match is the answer.

Code:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intn=250000+5; ll N,k;ll gcd (ll a,ll b) {returnB?GCD (b,a%b): A;} ll A[n],fac[n];inttot;intMain () {scanf ("%lld%lld",&n,&k);  for(intI=1; i<=k;i++) scanf ("%lld",&A[i]); ll G=gcd (a[k],n); //cout<< "GG" <<g<<endl;     for(LL i=1; i*i<=g;i++){        if(g%i==0) {fac[++tot]=i; if(i!=g/i) fac[++tot]=g/i; }}sort (FAC+1, fac+tot+1);  for(intI=1; i<=tot;i++){        //cout<<fac[i]<< "";        BOOLFl=true;  for(intj=1; j<=k-1; j + +){            if(a[j]%fac[i]==0) {FL=false; Break; }        }        if(fl) {printf ("%lld", N/fac[i]);return 0; }    }    //cout<< "Over" <<endl;    return 0;}

[POI2011] Sej-strongbox

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.