Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.TreeSet;
- class MonkAndAtm {
- static final int MAX=10000001;
- static int factors[]=new int[MAX];
- public static void main(String[] args) throws IOException {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- int t,n;
- t=Integer.parseInt(br.readLine());
- initFactors();
- StringBuilder sbr=new StringBuilder();
- while (t-- > 0) {
- n=Integer.parseInt(br.readLine());
- sbr.append(solve(n)+"\n");
- //System.out.println(solve(n));
- }
- System.out.print(sbr.toString());
- }
- private static int solve(int n) {
- for (int m = n; m>0 ; m--) {
- if (n%m==0)
- {
- int temp=countFactors(m);
- if (isPowerOf2(temp))
- return temp;
- }
- }
- return 0;
- }
- private static int countFactors(int n) {
- int m=n,count=0,ans=1;
- while (n>1){
- count=0;
- int x=factors[n];
- while (n%x==0){
- count++;
- n/=x;
- }
- ans*=(count+1);
- }
- return ans;
- }
- public static boolean isPowerOf2(int n) {
- if (n==1)
- return true;
- if ((n&1)==1 || n==0)
- return false;
- return isPowerOf2(n >> 1);
- }
- private static void initFactors() {
- for (int i = 2; i < MAX; i+=2) {
- factors[i]=2;
- }
- int sqrt= (int) Math.sqrt(MAX);
- for (int i = 3; i <MAX; i+=2) {
- if (factors[i]==0){
- factors[i]=i;
- for (int j =i*i; j>0 && j <MAX ; j+=2*i) {
- if (factors[j]==0)
- factors[j]=i;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment