USACO-Section 1.3 Prime Cryptarithm(枚举)

该博客介绍了如何解决一种名为 PRIME CRYPTARITHM 的乘法问题,其中涉及使用特定集合(如 {2,3,5,7})中的素数替换星号位置的数字。文章强调了美国学校教授的多位数乘法规则,并提供了一个程序(crypt1)来找到所有可能的解决方案,针对任何给定的非零单个数字子集。输入和输出格式进行了说明,并给出了样例输入和输出。" 112221808,10325468,Termux通过SSH连接服务器及FHLINK打印服务器网络配置指南,"['网络配置', '打印服务器', 'SSH连接', 'Termux', '打印机网络']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.

      * * *
   x    * *
    -------
      * * *         <-- partial product 1
    * * *           <-- partial product 2
    -------
    * * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.

The partial products must be three digits long, even though the general case (see below) might have four digit partial products. 

********** Note About Cryptarithm's Multiplication ************ 
In USA, children are taught to perform multidigit multiplication as described here. Consider multiplying a three digit number whose digits are 'a', 'b', and 'c' by a two digit number whose digits are 'd' and 'e':

[Note that this diagram shows far more digits in its results than
the required diagram above which has three digit partial products!]

          a b c     <-- number 'abc'
        x   d e     <-- number 'de'; the 'x' means 'multiply'
     -----------
p1      * * * *     <-- product of e * abc; first star might be 0 (absent)
p2    * * * *       <-- product of d * abc; first star might be 0 (absent)
     -----------
      * * * * *     <-- sum of p1 and p2 (e*abc + 10*d*abc) == de*abc

Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.

Write a program that will find all solutions to the cryptarithm above for any subset of supplied non-zero single-digits.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:N, the number of digits that will be used
Line 2:N space separated non-zero digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

5
2 3 4 6 8

OUTPUT FORMAT

A single line with the total number of solutions. Here is the single solution for the sample input:

      2 2 2
    x   2 2
     ------
      4 4 4
    4 4 4
  ---------
    4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1

题目给n个数字,要求找出所有符合要求的    3位数(abc)乘以2位数(de)   的组合

①abc和de由规定数字组成

②abc*d与abc*e的结果是三位数且由规定数字组成

③abc*de的结果是四位数且由规定数字组成


由于最多只有9个数字,所以枚举每个数字即可


/*
ID: your_id_here 
PROG: crypt1
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int ans,n,num[10],a,b,c,t1,t2,fir;
bool used[10];

inline bool judge(int x) {//判断数x是否只含有规定数字
    while(x) {
        if(!used[x%10])
            return false;
        x/=10;
    }
    return true;
}

int main() {
    int i,j,k,l,m;
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
    while(1==scanf("%d",&n)) {
        memset(used,false,sizeof(used));
        for(i=0;i<n;++i) {
            scanf("%d",num+i);
            used[num[i]]=true;
        }
        ans=0;
        for(i=0;i<n;++i) {
            if((a=num[i])!=0)
                for(j=0;j<n;++j) {
                    b=num[j];
                    for(k=0;k<n;++k) {
                        fir=a*100+b*10+num[k];
                        for(l=0;l<n;++l) {
                            if((c=num[l])!=0&&(t1=fir*c)<1000&&judge(t1))
                                for(m=0;m<n;++m) {
                                    if((t2=fir*num[m])<1000&&judge(t2)&&judge(t1*10+t2))
                                        ++ans;
                                }
                        }
                    }
                }
        }
        printf("%d\n",ans);
    }
    return 0;
}




### USACO 1327 Problem Explanation USACO 1327涉及的是一个贪心算法中的区间覆盖问题。具体来说,这个问题描述了一组奶牛可以工作的班次范围,并要求找出最少数量的奶牛来完全覆盖所有的班次。 对于此类问题的一个有效方法是采用贪心策略[^1]。首先按照区间的结束时间从小到大排序这些工作时间段;如果结束时间相同,则按开始时间从早到晚排列。接着遍历这个有序列表,在每一步都尽可能选择最早能完成当前未被覆盖部分的工作时段。通过这种方式逐步构建最终解集直到所有的时间段都被覆盖为止。 为了提高效率并防止超时错误,建议使用`scanf()`函数代替标准输入流操作符`cin`来进行数据读取处理[^2]。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start; int end; }; bool compareIntervals(const Interval& i1, const Interval& i2) { return (i1.end < i2.end || (i1.end == i2.end && i1.start < i2.start)); } int main() { vector<Interval> intervals = {{1, 7}, {3, 6}, {6, 10}}; sort(intervals.begin(), intervals.end(), compareIntervals); int currentEnd = 0; int count = 0; for (const auto& interval : intervals) { if (interval.start > currentEnd) break; while (!intervals.empty() && intervals.front().start <= currentEnd) { if (intervals.front().end >= interval.end) { interval = intervals.front(); } intervals.erase(intervals.begin()); } currentEnd = interval.end; ++count; if (currentEnd >= 10) break; // Assuming total shift length is known. } cout << "Minimum number of cows needed: " << count << endl; } ``` 此代码片段展示了如何实现上述提到的方法解决该类问题。需要注意的是实际比赛中可能还需要考虑更多边界条件以及优化细节以满足严格的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值