zoj 3008 Gold Coins

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HYNU_zhizuzhe/article/details/50009417

As a merchant, Bill likes gold coins very much. One day, when he was counting his gold coins again and again, a witch came into his house. The witch told him that she could change his n coins to n^m. Of course, Bill knew how quickly the number was growing if m was more than 1. So he felt very happy. However, there was a precondition that Bill should answer the witch a question before he gained the coins as to the witch. Bill could use t(1 ≤ t ≤ n) bags to store the n^m coins and the num of coins in every bag should be equal. The question mentioned above was that how many ways there were to store the coins. Bill was disappointed for he wasn't able to solve it, but he also didn't want to give up. So he needed your help, maybe you would share the gold coins after solving it.

Input

Every test case contains two numbers n(1 ≤ n ≤ 10^8) and m(1 ≤ m ≤ 10), process to the end of file.

Output

For every case, print the number of storing ways in a line.

Sample Input

6 2

Sample Output

5

题意:n^m在1-n的范围内有多少个因子

思路:先求n的质因数,再搜索就ok了

#include<iostream>
#include<cstdio>
using namespace std;

#define LL long long

int n,a[100],b[100],k,ans;

void dfs(int d,LL sum)
{
	if(d>=k) {
		ans++;
		return ;
	}
	for(int i=0;i<=b[d];i++) {
		if(i) sum*=a[d];
		if(sum>n) break;
		dfs(d+1,sum);
	}
}

int main()
{
	int m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,t=n;
		ans=0; k=0;
		for(i=2;i*i<=n;i++) {
			if(t%i==0) {
				a[k]=i;
				b[k]=0;
				while(t%i==0) {
					b[k]++;
					t/=i;
				}
				b[k++]*=m;
			}
		}
		if(t>1) {
			a[k]=t;
			b[k++]=m;
		}
		dfs(0,1);
		printf("%d\n",ans);
	}
	return 0;
}

心得:1. 数据类型范围是循环的

            2. n的质因数在sqrt(n)-n的范围内只可能有一个,原因嘛,在sqrt(n)-n中的任意两个数相乘都会大于n

总结:凡是有关数的问题,优先往质因数方面思考


猜你喜欢

转载自blog.csdn.net/HYNU_zhizuzhe/article/details/50009417
ZOJ