位運算子(&,>>,^)的妙用
阿新 • • 發佈:2018-12-11
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:
-
D is one of the factors of P;
-
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; }