1543: Numbers
时间限制: 1 Sec 内存限制: 128 MB
提交: 445 解决: 106
[提交][状态][讨论版]
题目描述
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.
输入
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).
输出
For each test case, print the number of positive integers D that satisfies the rules.
样例输入
2
1
10
样例输出
1
2
提示
时间限制: 1 Sec 内存限制: 128 MB
提交: 445 解决: 106
[提交][状态][讨论版]
题目描述
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.
输入
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).
输出
For each test case, print the number of positive integers D that satisfies the rules.
样例输入
2
1
10
样例输出
1
2
提示
来源
心得:直接将n变为二进制并用数组存储起来
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int a[1001];
int is_f(LL p)
{
LL i=0;
while(p)
{
LL tp=p%2;
if(a[i]==tp) return 1;
p/=2; i++;
}
return 0;
}
int main(void)
{
int t;
LL p,num;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&p);
LL tp=p,i=0;
while(tp)
{
a[i]=tp%2;
i++;
tp/=2;
}
num=0;
for(i=1;i*i<=p;i++)
if(p%i==0)
{
if(is_f(i)) num++;
if(is_f(p/i)&&(i*i!=p)) num++;
}
printf("%lld\n",num);
memset(a,0,sizeof(a));
}
return 0;
}