{"id":508,"date":"2013-08-28T18:40:55","date_gmt":"2013-08-28T10:40:55","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=508"},"modified":"2013-08-28T18:40:55","modified_gmt":"2013-08-28T10:40:55","slug":"1059-prime-factors-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=508","title":{"rendered":"1059. Prime Factors (25)"},"content":{"rendered":"<p>Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p<sub>1<\/sub>^k<sub>1<\/sub>* p<sub>2<\/sub>^k<sub>2<\/sub>\u00a0*\u2026*p<sub>m<\/sub>^k<sub>m<\/sub>.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case which gives a positive integer N in the range of long int.<br \/>\n<b>Output Specification:<\/b><br \/>\nFactor N in the format N = p<sub>1<\/sub>^k<sub>1<\/sub>\u00a0* p<sub>2<\/sub>^k<sub>2<\/sub>\u00a0*\u2026*p<sub>m<\/sub>^k<sub>m<\/sub>, where p<sub>i<\/sub>&#8216;s are prime factors of N in increasing order, and the exponent k<sub>i<\/sub>\u00a0is the number of p<sub>i<\/sub>\u00a0&#8212; hence when there is only one p<sub>i<\/sub>, k<sub>i<\/sub>\u00a0is 1 and must NOT be printed out.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>97532468<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>97532468=2^2*11*17*101*1291<\/pre>\n<p>&nbsp;<br \/>\n=======================================================<br \/>\n\u5c31\u662f\u4e00\u4e2a\u627e\u8d28\u6570\u7684\u95ee\u9898\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\u5566~<br \/>\n\u5207\u8bb0\u4ece\u5c0f\u7684\u8fd9\u5934\u5f00\u59cb\u5c1d\u8bd5\uff0c\u56e0\u4e3a\u8bd5\u60f3\u4e00\u4e0b\uff0c\u9664\u63892\uff0c3\u8fd9\u79cd\u5c0f\u6570\u5b57\u4e4b\u540e\uff0c\u9700\u8981\u5c1d\u8bd5\u7684\u660e\u663e\u5c11\u4e86N\u591a\uff01<\/p>\n<pre>\n#include <stdio.h>\n#include <map>\nusing namespace std;\nconst int prime[] =\n{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };\nconst int prime_length = 25;\nmap<long, bool> primeMap;\nbool isPrime(long val)\n{\n    for(int i=0; i<prime_length; i++)\n    {\n        if(prime[i] == val)\n            return true;\n        if(val%prime[i] == 0)\n            return false; \/\/\u80fd\u9664\u5f97\u5c3d\u53c8\u4e0d\u76f8\u7b49\uff0c\u80af\u5b9a\u662f\u5408\u6570\n    }\n    if(val < 100)\n        return false;\n    \/\/\u6700\u540e\u4e00\u7ebf\u5e0c\u671b...\n    if(primeMap.count(val) > 0)\n        return primeMap[val];\n    \/\/\u6ca1\u529e\u6cd5\u4e86\u53ea\u80fd\u82e6\u7b97\n    for(long x=val\/2; x>1; x--)\n    {\n        if(val%x == 0)\n        {\n            \/\/\u5408\u6570\n            primeMap.insert(pair<long,bool>(val, false));\n            return false;\n        }\n    }\n    primeMap.insert(pair<long,bool>(val, true));\n    return true;\n}\nmap<long, long> factorCountMap;  \/\/<\u7d20\u6570\uff0c\u6307\u6570>\nint main()\n{\n    long input;\n    scanf(\"%ld\", &input);\n    printf(\"%ld=\", input);\n    \/\/\n    if(input == 1)\n    {\n        printf(\"1\");\n        return 0;\n    }\n    \/\/\u4ece2\u5f00\u59cb\u8ba1\u7b97...\n    long divisor = 2;\n    while(input > 1)\n    {\n        if(!isPrime(divisor))\n        {\n            divisor++;\n            continue;\n        }\n        if(input%divisor == 0)\n        {\n            \/\/\u6709\u8fd9\u4e2a\u7d20\u6570\u56e0\u5b50\n            if(factorCountMap.count(divisor) > 0)\n            {\n                factorCountMap[divisor] = factorCountMap[divisor] + 1;\n            }\n            else\n            {\n                factorCountMap[divisor] = 1;\n            }\n            input \/= divisor;\n        }\n        else\n            divisor++;\n    }\n    map<long,long>::iterator it = factorCountMap.begin();\n    for(; it!=factorCountMap.end(); it++)\n    {\n        if(it != factorCountMap.begin())\n            printf(\"*\");\n        pair<long,long> p = *it;\n        long factor = p.first;\n        long exponent = p.second;\n        printf(\"%ld\", factor);\n        if(exponent > 1)\n            printf(\"^%ld\", exponent);\n    }\n    return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2\u00a0*\u2026*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of long int. Output Specification: Factor N in the format N = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=508"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}