位运算符(&,>>,^)的妙用

版权声明:个人做题总结专用~ https://blog.csdn.net/tb_youth/article/details/84948518

1543: Numbers
Time Limit: 1 Sec Memory Limit: 128 MB

[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

HINT

Source

首届全国中医药院校大学生程序设计竞赛
AC_code:

#include <stdio.h>
int a[200];
bool judge(int aim)
{
    int b,i = 0;
    while(aim)
    {
        b = aim&1;
        if(!(b^a[i]))//相同位置上只要有一位二进位相同就满足条件(条件或写成b==a[i])
            return true;
        aim>>=1;
        i++;
    }
    return false;
}
int main()
{
    int T;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            long p,j = 0,sum = 0,num;
            scanf("%ld",&p);
            num = p;
            while(p)
            {
                a[j++] = p&1;//p的二进制各位数字存入数组a
                p>>=1;
            }
            for(int i = 1; i*i <= num; i++)
            {
                if(!(num%i))
                {
                    if(judge(i))
                        sum++;
                    if(judge(num/i)&&(i*i!=num))//如果i是num的因子必有num/i也是num的因子
                        sum++;
                }
            }
            printf("%ld\n",sum);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tb_youth/article/details/84948518
今日推荐