UVa 1635-irrelevant elements-[decomposition factorization]

Source: Internet
Author: User
Tags integer numbers

Young Cryptoanalyst Georgie was investigating different schemes of generating random integer numbersranging from 0 to m−1 .  He thinks that standard random number generators was not good enough, Sohe have invented his own scheme that's intended to Bring more randomness into the generated numbers. First, Georgie chooses N and generates n random integer numbers ranging from 0 to m−1. Letthe numbers generated be a1, A2, ..., an. After this Georgie calculates the sums of all pairs of adjacentnumbers, and replaces the initial array with the array of s UMS, thus getting n−1 numbers:a1 +a2, A2 +a3, ..., an−1 + an. Then he applies the same procedure to the new array, getting n−2 numbers. Theprocedure is repeated until only one number is left. This was then taken modulo m. That givesthe result of the generating procedure. Georgie have proudly presented this scheme to he computer science teacher, but is pointed out thatthe scheme have many DRA Wbacks. One important drawback is the FACt. The result of the proceduresometimes does not even depend on some of the initially generated numbers. For example, if n = 3and m = 2 and then the result does not depend on A2. Now Georgie wants to investigate this phenomenon. He calls the i-th element of the initial arrayirrelevant if the result of the generating procedure does not depend on AI. He considers various N and
M and wonders which elements is irrelevant for these parameters. Help him to find it out.InputInput file contains several datasets. Each datasets have N and m (1≤n≤100 000, 2≤m≤109) in Asingle line.OutputOn the first line of the "output for each dataset" print the number of irrelevant elements of the Initialarray for given N a nd m. On the second, line print, all such I, i-th element is irrelevant. Numberson the second line must is printed in the ascending order and must is separated by spaces.Sample Input3 2Sample Output12

Problem Solving Ideas:

Decomposition factorization only with sieve 10^5 within the prime number, do not forget to be greater than 10^5 of the quality factor stored separately.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <vector>5#include <cmath>6#include <cstring>7#include <ctime>8 using namespacestd;9 #defineMAXN 100010Ten #defineTime_ printf ("%f", Double (Clock ())/clocks_per_sec) One intVIS[MAXN]; Avector<int>Prime; -vector<int>FM; - intE_M[MAXN]; the intE_C[MAXN]; - intn,m; - voidPre () { -     intM=SQRT (MAXN) +1; +      for(intI=2; i<m;i++){ -         if(!Vis[i]) { +              for(intj=i*i;j<maxn;j+=i) { Avis[j]=1; at             } -         } -     } -      for(intI=2; i<maxn;i++) -         if(!Vis[i]) prime.push_back (i); - } in voidCal_e (intMintE_M[MAXN],intd) { -     intt=m; to      for(intI=0; I<fm.size (); i++){ +          while(t%fm[i]==0){ -e_m[i]+=D; theT/=Fm[i]; *         } $         if(t==1) Break;Panax Notoginseng     } - } the BOOLjudge () { +      for(intI=0; I<fm.size (); i++) A         if(E_m[i]>e_c[i])return false; the     return true; + } - intMainintargcConst Char*argv[]) { $ pre (); $      while(SCANF ("%d%d", &n,&m) = =2){ -memset (E_m,0,sizeofe_m); -memset (E_c,0,sizeofe_c); the fm.clear (); -Cal_e (M,e_m,1);Wuyi          thevector<int>ans; -          Wu         intt=m; -         intj=0; About          for(intI=0; I<prime.size (); i++){ $             if(t%prime[i]==0){ - Fm.push_back (Prime[i]); -                  while(t%prime[i]==0){ -e_m[j]++; AT/=Prime[i]; +                 } theJ + +; -             } $             if(t==1) Break; the         } the         if(t!=1) {Fm.push_back (t); e_m[j]=1;} the          for(intk=1; k<n;k++){ theCal_e (N-k,e_c,1); -Cal_e (k,e_c,-1); in             if(judge ()) { the Ans.push_back (k); the             } About         } theprintf"%d\n",(int) ans.size ()); the         if(Ans.size () >0){ theprintf"%d", ans[0]+1); +              for(intI=1; I<ans.size (); i++) -printf"%d", ans[i]+1); the         }Bayi         //else printf ("\ n"); theprintf"\ n"); the         //time_; -     } -      the     return 0; the}

UVa 1635-irrelevant elements-[decomposition factorization]

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.