Description
DongDong is fond of numbers, and he has a positive integer P. Meanwhile, there is a rule that is:
A positive integer D that satisfies the following rules:
1. D is one of the factors of P
2. D and P have a same bit at least under the binary system.
So DongDong wants to know how many positive integers D there are.
Input
The first line contains a positive integer T (T<=1000), which means the number of test cases. Then comes T lines, each line contains a positive integer P (1<=P<=1000000000).
Output
For each test case, print the number of positive integers D that satisfies the rules.
Sample Input
2
1
10
Sample Output
1
2
题意就是输入一个T,然后有T组数据,输入数字P,问有多少个数组D满足即时P的因数,且D和P的二进制存在相同的位(存在一位就满足)。
嗯,首先看到这个题,老规矩先暴力一把然后不出所料的TEL,然来想了一想,既然要找因数,比如8的时候,找出2的同时就找出4了,然后很高兴的把循环降到了P/2,然后WA了!仔细看看原来把循环降到sqrt(P),才对。。。。。顺着思路继续写了一遍,继续WA,不应该啊,然后继续想,终于知道了在等于sqrt(P)的时候,会算两遍因数,所以改进了一些,A了。
AC代码:
#include<iostream>
#include<cmath>
using
namespace
std;
int
main()
{
long
long
t,a[100000],b[100000],k,n,sum,j,m,q,p;
while
(cin>>t)
{
while
(t--)
{
sum=0;
cin>>n;
if
(n==1)
{
cout<<1<<endl;
continue
;
}
q=n;
j=0;
while
(n!=0)
{
a[j]=n%2;
// cout<<a[j];
n=n/2;
j++;
}
//cout<<endl;
for
(
int
i=1;i<=
sqrt
(q);i++) //大大降低时间复杂度。
{
if
(q%i==0) //满足i是q的因数
{
p=i;
k=q/i;
m=0;
//cout<<i<<endl;
while
(k!=0)
{
b[m]=k%2;
k=k/2;
//cout<<b[m];
m++;
}
//cout<<endl;
for
(
int
i=0;i<m;i++)
{
if
(a[i]==b[i])
{
sum++;
break
;
}
}
k=p;
m=0;
while
(k!=0)
{
b[m]=k%2;
k=k/2;
//cout<<b[m];
m++;
}
//cout<<endl;
for
(
int
i=0;i<m;i++)
{
if
(a[i]==b[i])
{
sum++;
break
;
}
}
}
if
(p==
sqrt
(q)) //存在算两遍的情况,自减一次就行了;
{
sum--;
}
}
cout<<sum<<endl;
}
}
return
0;
}