hdu 2441-ACM(Array Complicated Manipulation)(数学)
2011-08-28 23:54
405 查看
ACM(Array Complicated Manipulation)[b]Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 333 Accepted Submission(s): 63 [/b] Problem Description Given an infinite array of integers 2,3,.... Now do some operations on it. The operation is to choose a minimum number from the array which is never been chosen, then change the status of its multiples excluding itself, i.e remove the multiples of the chosen number if they are in the array , otherwise add it to the array.keep the order after change. For instance, the first step, choose number 2, change the status of 4, 6, 8, 10... They are all removed from the array. The second step, choose 3, change the status of 6, 9, 12, 15... Pay attention: 9 and 15 are removed from the array while 6 and 12 are added to the array. Input Every line contains an integer n. The zero value for n indicates the end of input. Output Print "yes" or "no" according whether n is in the array. Sample Input 2 30 90 0 Sample Output yes yes no HintThe number n never has a prime factor greater than 13000000, but n may be extremely large. |
解法:这道题说起来有点坑爹,题目的数据范围没具体说明,害我开到__int64都是错误的,居然必须用大数。其次,Hint中说这个数的最大素因子不会超过1300W,但其实这个数只有2^16这么大,我尝试用1300W去做,结果超时了。
这道题打表之后不难发现,只要这个数的因子中存在平方数,那么这个数就不在数组里(我是打表发现这个规律的)。那么,我们只要把这个大数分解成素因子幂次方乘积(
N=p1^e1*p2^e2*...*pn^en)的方式,只要存在一个幂次方大于等于2的,那么这个数就有一个因子是平方数(就不存在)。否则就存在。
此题注意高精度方面的处理即可。
#include<iostream> #include <CMATH> #include <cstring> #include <cstdio> using namespace std; #define N 66000 bool is ; __int64 prm[66000]; int getprm(int n){ int i, j, k = 0; int s, e = (int)(sqrt(0.0 + n) + 1); memset(is, 1, sizeof(is)); prm[k++] = 2; is[0] = is[1] = 0; for (i = 4; i < n; i += 2) is[i] = 0; for (i = 3; i < e; i += 2) if (is[i]) { prm[k++] = i; for (s = i * 2, j = i * i; j < n; j += s) is[j] = 0; // 因为j是奇数,所以+奇数i后是偶数,不必处理! } for ( ; i < n; i += 2) if (is[i]) prm[k++] = i; return k; // 返回素数的个数 } bool div(char *p,int n) { char temp[1000]; int i,sum=0,len=0; for(i=0;p[i]!=0;i++) { sum=sum*10+p[i]-'0'; temp[len++]=sum/n+'0'; sum%=n; } temp[len]=0; if(sum==0) { for(i=0;temp[i]=='0';i++); strcpy(p,temp+i); return 1; } else return 0; } int main() { // freopen("test2.out","r",stdin); int cnt=getprm(66000); int i,n; char str[1010]; while (scanf("%s",&str)&&str[0]!='0') { bool judge=true; if(strcmp(str,"1")==0) { printf("no\n"); continue; } for(i=0;i<cnt;i++) { int sum=0; while(div(str,prm[i])) { sum++; if(sum>1) { judge=false; break; } } } if(judge) printf("yes\n"); else printf("no\n"); } return 0; }
相关文章推荐
- hdu 2441(ACM(Array Complicated Manipulation))
- HEU 5084 ACM(Array Complicated Manipulation)
- HDU2441 ACM(Array Complicated Manipulation)
- HDU 5532 Almost Sorted Array(最长非递减子序列 模板题)——2015ACM/ICPC亚洲区长春站
- HDU 3296 & POJ 3138 Acm Team Section(数学)
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
- HDU 4810 Wall Painting(组合数学 + 位运算)——2013ACM/ICPC亚洲区南京站现场赛
- HDU 4565 -- So Easy! 数学 && 2013 ACM-ICPC 长沙赛区全国邀请赛 A题
- HDU 5365 LCP Array (数学 逻辑 对我来说非常困难)
- HDU 5512 (ACM 2015 沈阳)Pagodas [基础数学]
- 2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)HDU 5532 Almost Sorted Array
- hdu 5047 Sawtooth 2014 ACM/ICPC Asia Regional Shanghai Online 数学
- HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~
- http://acm.hdu.edu.cn/showproblem.php?pid=2441 犀利的证明
- HDU 4572 Bottles Arrangement(数学推公式)——2013 ACM-ICPC长沙赛区全国邀请赛
- ACM学习历程—HDU 5073 Galaxy(数学)
- HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)
- hdu 5587 Array 数学题
- HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array
- HDU - 6197 array array array (2017 ACM-ICPC 亚洲区 (沈阳赛区) 网络赛 1004)