思路十分简单,就是计算一下 n / ((m)^(n-1));
代码如下:
int cnt,ans[100]; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } void cal(int x) { for(int i = 0; i < cnt; ++i) ans[i] *= x; int t = 0; for(int i = 0; i < cnt; ++i) { ans[i] += t; t = ans[i]/10000; ans[i] %=10000; } while(t) { ans[cnt++] = t%10000; t /= 10000; } } int main() { int t,n,m,z; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); memset(ans,sizeof(ans)); z = n; cnt = 1; ans[0] = 1; for(int i = 0; i < n-1; ++i) { int g = gcd(m,z); z /= g; cal(m/g); } printf("%d/",z); for(int i = cnt-1; i >= 0; --i) i==cnt-1?printf("%d",ans[i]):printf("%.4d",ans[i]); printf("\n"); } return 0; }