[USACO1.3]牛式 Prime Cryptarithm

版权声明:转载无所谓的。。。 https://blog.csdn.net/xuxiayang/article/details/82953472

大意

给定一些数字问下面这个方程的解中只包含这些数字的方案数

( 1 0 2 x 1 + 1 0 1 x 2 + 1 0 0 x 3 ) x 4 + ( 1 0 2 x 1 + 1 0 1 x 2 + 1 0 0 x 3 ) 10 x 5 = 1 0 3 x 6 1 0 2 x 7 1 0 1 x 8 1 0 0 x 9 (10^2x_1+10^1x_2+10^0x_3)x_4+(10^2x_1+10^1x_2+10^0x_3)10x_5=10^3x_610^2x_710^1x_810^0x_9


思路

因为 ( 1 0 2 x 1 + 1 0 1 x 2 + 1 0 0 x 3 ) x 4 (10^2x_1+10^1x_2+10^0x_3)x_4 这一陀最多是999,然后我们乘法结合+分配一下发现就是乘上一个二位数,这个时候我们枚举一下就好了啦。


代码

/*
ID:hzbismy1
LANG:C++
TASK:crypt1
*/
#include<cstdio>
using namespace std;bool ok[10];int n,a,t1,t2,t3,ans;
inline bool check(register int x){while(x){if(!ok[x%10])return false;x/=10;}return true;}
signed main()
{
	freopen("crypt1.in","r",stdin);
	freopen("crypt1.out","w",stdout);
	scanf("%d",&n);
	for(register int i=1;i<=n;i++) scanf("%d",&a),ok[a]=1;
	for(register int i=111;i<1000;i++)//枚举乘数
	{
		if(!check(i)) continue;
		for(register int j=11;j<99;j++)//枚举乘数
		{
			if(!check(j)) continue;
			t1=i*(j%10);t2=i*(j/10);t3=t1+(t2<<3)+(t2<<1);
			if(!check(t1)||!check(t2)||!check(t3)) continue;//判断一下合不合法
			if(t1>999||t3>9999)continue;//判断一下有没有超出范围
			ans++;
		}
	}
	printf("%d\n",ans);//输出
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/82953472