zoj 3008 Gold Coins

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

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值