HDU 4432 Sum of divisors 质因数分解
2014-09-10 21:08
232 查看
题意:给出n,m,求出n的所有因子在m进制下,每位的数字的平方的和。
思路:直接用根号n的朴素求所有因数的方法。
也可以求出所有的质因子,再还原因子。
代码如下:
思路:直接用根号n的朴素求所有因数的方法。
也可以求出所有的质因子,再还原因子。
代码如下:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; bool valid[100100]; void getprime(int n,int &tot,int ans[]) { tot=0; for (int i=2;i<=n;i++) { if (valid[i]) { tot++; ans[tot]=i; } for (int j=1;((j<=tot) && (i*ans[j]<=n));j++) { valid[i*ans[j]]=false; if (i%ans[j]==0) break; } } } void factor(int n,int a[],int b[],int &tot,int ss[],int sstot) { int i,now; tot=0; now=n; for (i=1;i<=sstot;i++) if (now%ss[i]==0) { a[++tot]=ss[i]; b[tot]=0; while (now%ss[i]==0) { ++b[tot]; now/=ss[i]; } } if (now!=1) { a[++tot]=now; b[tot]=1; } } int ss[100100],sstot; int a[100100],b[100100],tot; int n,m; long long sum; void getsum(int num) { int x; while (num) { x=num%m; sum+=x*x; num/=m; } } void found(int x,int num) { int i; int temp=num; if (x==tot+1 && num!=0) { getsum(num); return; } for (i=0;i<=b[x];i++) { found(x+1,temp); temp*=a[x]; } } int ans[100100],anstop; int main() { memset(valid,true,sizeof(valid)); getprime(100000,sstot,ss); while (scanf("%d%d",&n,&m)!=EOF) { sum=0; factor(n,a,b,tot,ss,sstot); found(1,1); anstop=0; while (sum) { anstop++; ans[anstop]=sum%m; sum/=m; } for (int i=anstop;i>=1;i--) { if (ans[i]>=10) { char ch; ch=ans[i]-10+'A'; printf("%c",ch); } else { printf("%d",ans[i]); } } printf("\n"); } return 0; }
相关文章推荐
- B - Sum of divisors HDU - 4432
- HDU 4432 Sum of divisors
- HDU 4432 Sum of divisors (水题,进制转换)
- HDU 4432 Sum of divisors (进制模拟)
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
- HDU 4432 Sum of divisors 2012 Asia Tianjin Regional Contest
- HDU 4432 Sum of divisors
- HDU 4432 Sum of divisors (进制模拟)
- hdu 4432 Sum of divisors
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
- HDU 4432 Sum of divisors (进制模拟)
- hdu 4432 Sum of divisors
- hdu 4432 Sum of divisors
- HUD 4432 Sum of divisors 天津现场赛B题
- hdu-4432-Sum of divisors
- HDU—— 4432 Sum of divisors
- hdu 4432 Sum of divisors(十进制转其他进制)
- hdu 4432 Sum of divisors(模拟)
- hdoj 4432 Sum of divisors
- HDU 4432 Sum of divisors (进制转换模板)