1543: Numbers
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 323 Solved: 69
[ Submit][ Status][ Web Board]
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
【解析】
此题要求统计你n的因子当中有几个在二进制位当中的只要有一位数相等的就行。让你输出一共有几个,这个小细节我们就要注意了。因为p取最大值时很大...
#include<cstdio>
using namespace std;
int a[100];
int check(int n)//判断二进制位当中有没有相等的
{
int r;
for(int i=0;n!=0;i++,n=n>>1)//n右移一位相当于n/2
{
r=n&1;//如果n的二进制位当中是1的则还是1如果是0的则变成0了
if(r==a[i])
return 1;
}
return 0;
}
int main()
{
int t,i;
long long n,m,count;
scanf("%d",&t);
while(t--)
{ count=0;
scanf("%lld",&n);
m=n;
for(i=0;n!=0;i++)
{
a[i]=n&1;//把n的二进制位写入
n=n>>1;
}
for(i=1;i*i<=m;i++)
{
if(m%i==0)
{
if(check(i))
count++;//这个是检测因子的好办法了省时间
if(check(m/i)&&(i*i!=m))
count++;
}
}
printf("%lld\n",count);
}
return 0;
}