2015百度之星初赛2 1004 魔法因子(暴力+数学)

Problem Description
有人说:人类是自己一步步进化的,而数学是上帝亲手创造的。度度熊最近也正沉醉于数学之美中,它发现了一种神奇的数字,取名曰:魔法因子。

将因子记为X,如果有一些整数与这些因子做乘法后,结果仍然是整数,同时,结果数字的首位和末位会换交换位置,而其他位置上的数字恰好不变!这时X被认为是一个魔法因子。需要注意的是,用来相乘的这些整数不会含有前导0,但是如果交换的结果有前导0,又恰好是乘法的结果,这时仍然认为X是这个整数的魔法因子。度度熊认为1不是个魔法因子,因为所有的数都可以符合这个条件,这一点都不好玩。

比如,X = 3.1312,有1875 * 3.1312 = 5871。

度度熊现在希望知道对于一个因子X,究竟有多少个整数可以满足这个条件,使其成为魔法因子。由于它还不会长度超过10位数的乘法,只需要求出长度不超过10的整数即可。
 

Input
第一行一个整数T,表示包含T组数据。
每组数据包含一个实数
X(0<X<10,X1) ,同时为了避免精度问题,小数点后的数字不会超过6位。
 

Output
每组数据,对于每组数据,先输出一行

Case #i:

然后输出符合条件的整数的个数,如果个数不为0,在第二行输出所有符合条件的整数,按数字大小升序排列,用空格隔开,如果个数为0,只输出一行。

这道题看起来比较麻烦,实际上只要枚举首位数字和末尾数字以及位数即可,注意消除浮点误差的关键一步
LL nx = x * 1000000 + 0.5

代码如下
#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack> 
#include<string>
#include<map> 
#include<set>
#define eps 1e-6 
using namespace std;  
#define LL long long  

//const int maxn = ;
const int INF = 0x3f3f3f3f;
//freopen("input.txt", "r", stdin);
const LL po[] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};


int main() {
	int T, kase = 0; scanf("%d", &T);
	while(T--) {
		double x; cin >> x;
		LL nx = x * 1000000 + 0.5; LL changshu = 1000000; //将浮点数转换为整形 
		vector<LL> ans;
		LL power, firnum, lasnum;
		for(power = 0; power < 9; power++) {
			for(firnum = 1; firnum < 10; firnum++) {
				for(lasnum = 0; lasnum < 10; lasnum++) {
					if((lasnum*(po[power]*changshu-nx)+firnum*(changshu-po[power]*nx)) % (nx-changshu) != 0) continue; 
					LL mid = (lasnum*(po[power]*changshu-nx) + firnum*(changshu-po[power]*nx) )/ (nx-changshu);
					if(mid % 10 != 0) continue;
					if(mid >= po[power] || mid < 0) continue;
					ans.push_back(firnum*po[power] + mid + lasnum);
				}
			}
		} 
		cout << nx << endl;
		printf("Case #%d:\n", ++kase);
		int sz = ans.size();
		if(!sz) puts("0");
		else {
			cout << sz << endl;
			cout << ans[0];
			for(int i = 1; i < sz; i++) cout << " " << ans[i];
			cout << endl;
		}  
	}
	return 0;
} 












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值