|
1014 | 1014 | },
|
1015 | 1015 | {
|
1016 | 1016 | "cell_type": "code",
|
1017 |
| - "execution_count": 21, |
| 1017 | + "execution_count": 1, |
1018 | 1018 | "metadata": {},
|
1019 | 1019 | "outputs": [
|
1020 | 1020 | {
|
1021 | 1021 | "name": "stdout",
|
1022 | 1022 | "output_type": "stream",
|
1023 | 1023 | "text": [
|
1024 | 1024 | "1\n",
|
1025 |
| - "1 3 5\n", |
1026 |
| - "8\n" |
| 1025 | + "8\n", |
| 1026 | + "-1865008399 -524833575 -1162539990 -1003374718 -1677272376 -2046456104 -689632770 -688705725\n", |
| 1027 | + "854\n" |
1027 | 1028 | ]
|
1028 | 1029 | }
|
1029 | 1030 | ],
|
|
1054 | 1055 | " print(count)"
|
1055 | 1056 | ]
|
1056 | 1057 | },
|
| 1058 | + { |
| 1059 | + "cell_type": "code", |
| 1060 | + "execution_count": 9, |
| 1061 | + "metadata": {}, |
| 1062 | + "outputs": [ |
| 1063 | + { |
| 1064 | + "name": "stdout", |
| 1065 | + "output_type": "stream", |
| 1066 | + "text": [ |
| 1067 | + "1\n", |
| 1068 | + "8\n", |
| 1069 | + "-1865008399 -524833575 -1162539990 -1003374718 -1677272376 -2046456104 -689632770 -688705725\n", |
| 1070 | + "800\n" |
| 1071 | + ] |
| 1072 | + } |
| 1073 | + ], |
| 1074 | + "source": [ |
| 1075 | + "if __name__ == '__main__':\n", |
| 1076 | + " n = int(input())\n", |
| 1077 | + " while n != 0:\n", |
| 1078 | + " p = int(input())\n", |
| 1079 | + " lis = [int(x) for x in input().split()]\n", |
| 1080 | + " bits = 0\n", |
| 1081 | + " for i in range(0, 32):\n", |
| 1082 | + " k = 0\n", |
| 1083 | + " for j in range(0, len(lis)):\n", |
| 1084 | + " if lis[j] & (1 << i):\n", |
| 1085 | + " k = k + 1\n", |
| 1086 | + " bits += k * (len(lis) - k)\n", |
| 1087 | + " print(2 * bits % 1000000007)\n", |
| 1088 | + " n = n-1" |
| 1089 | + ] |
| 1090 | + }, |
| 1091 | + { |
| 1092 | + "cell_type": "markdown", |
| 1093 | + "metadata": {}, |
| 1094 | + "source": [ |
| 1095 | + "### [Bleak Numbers](https://practice.geeksforgeeks.org/problems/bleak-numbers/0/?track=sp-bit-magic&batchId=152)\n", |
| 1096 | + "- Company Tags : SAP Labs\n", |
| 1097 | + "- Difficulty: Medium\n", |
| 1098 | + "- Marks: 4\n", |
| 1099 | + "\n", |
| 1100 | + "***Given an integer, check whether it is Bleak or not.***\n", |
| 1101 | + "\n", |
| 1102 | + "***A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x + [countSetBits(x)](http://www.geeksforgeeks.org/count-set-bits-in-an-integer/) is not equal to n for any non-negative number x.***\n", |
| 1103 | + "\n", |
| 1104 | + "***Examples :***\n", |
| 1105 | + "\n", |
| 1106 | + "3 is not Bleak as it can be represented\n", |
| 1107 | + "as 2 + countSetBits(2).\n", |
| 1108 | + "\n", |
| 1109 | + "4 is t Bleak as it cannot be represented \n", |
| 1110 | + "as sum of a number x and countSetBits(x)\n", |
| 1111 | + "for any number x.\n", |
| 1112 | + "\n", |
| 1113 | + "***Input:***\\\n", |
| 1114 | + "The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line. The first line of each test case contains a single integer N to be checked for Bleak.\n", |
| 1115 | + "\n", |
| 1116 | + "***Output:***\\\n", |
| 1117 | + "Print \"1\" or \"0\" (without quotes) depending on whether the number is Bleak or not.\n", |
| 1118 | + "\n", |
| 1119 | + "***Constraints:***\\\n", |
| 1120 | + "1 <= T <= 1000\\\n", |
| 1121 | + "1 <= N <= 10000\n", |
| 1122 | + "\n", |
| 1123 | + "***Example:***\\\n", |
| 1124 | + "***Input:***\\\n", |
| 1125 | + "3\\\n", |
| 1126 | + "4\\\n", |
| 1127 | + "167\\\n", |
| 1128 | + "3\n", |
| 1129 | + "\n", |
| 1130 | + "***Output:***\\\n", |
| 1131 | + "1\\\n", |
| 1132 | + "0\\\n", |
| 1133 | + "0" |
| 1134 | + ] |
| 1135 | + }, |
| 1136 | + { |
| 1137 | + "cell_type": "code", |
| 1138 | + "execution_count": 9, |
| 1139 | + "metadata": {}, |
| 1140 | + "outputs": [ |
| 1141 | + { |
| 1142 | + "name": "stdout", |
| 1143 | + "output_type": "stream", |
| 1144 | + "text": [ |
| 1145 | + "1\n", |
| 1146 | + "887\n", |
| 1147 | + "1\n" |
| 1148 | + ] |
| 1149 | + } |
| 1150 | + ], |
| 1151 | + "source": [ |
| 1152 | + "for _ in range(int(input())):\n", |
| 1153 | + " n=int(input())\n", |
| 1154 | + " for i in range(0,n+1,2):\n", |
| 1155 | + " if (i+bin(i).count('1'))==n:\n", |
| 1156 | + " print(0)\n", |
| 1157 | + " break\n", |
| 1158 | + " else:\n", |
| 1159 | + " print(1)" |
| 1160 | + ] |
| 1161 | + }, |
1057 | 1162 | {
|
1058 | 1163 | "cell_type": "code",
|
1059 | 1164 | "execution_count": 20,
|
|
1116 | 1221 | },
|
1117 | 1222 | {
|
1118 | 1223 | "cell_type": "code",
|
1119 |
| - "execution_count": 9, |
| 1224 | + "execution_count": 2, |
1120 | 1225 | "metadata": {},
|
1121 | 1226 | "outputs": [
|
1122 | 1227 | {
|
1123 | 1228 | "data": {
|
1124 | 1229 | "text/plain": [
|
1125 |
| - "'1100100'" |
| 1230 | + "'-0b10'" |
1126 | 1231 | ]
|
1127 | 1232 | },
|
1128 |
| - "execution_count": 9, |
| 1233 | + "execution_count": 2, |
1129 | 1234 | "metadata": {},
|
1130 | 1235 | "output_type": "execute_result"
|
1131 | 1236 | }
|
1132 | 1237 | ],
|
1133 | 1238 | "source": [
|
1134 |
| - "a=bin(100)[2:]\n", |
| 1239 | + "a=bin(-2)\n", |
1135 | 1240 | "a"
|
1136 | 1241 | ]
|
1137 | 1242 | },
|
| 1243 | + { |
| 1244 | + "cell_type": "code", |
| 1245 | + "execution_count": 4, |
| 1246 | + "metadata": {}, |
| 1247 | + "outputs": [ |
| 1248 | + { |
| 1249 | + "ename": "ValueError", |
| 1250 | + "evalue": "invalid literal for int() with base 2: '1b10'", |
| 1251 | + "output_type": "error", |
| 1252 | + "traceback": [ |
| 1253 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 1254 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 1255 | + "\u001b[0;32m<ipython-input-4-d59bf4de1a76>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1b10'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 1256 | + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 2: '1b10'" |
| 1257 | + ] |
| 1258 | + } |
| 1259 | + ], |
| 1260 | + "source": [ |
| 1261 | + "int('1b10',2)" |
| 1262 | + ] |
| 1263 | + }, |
1138 | 1264 | {
|
1139 | 1265 | "cell_type": "code",
|
1140 | 1266 | "execution_count": 8,
|
|
0 commit comments