UVA725 UVALive5362 Division

Regionals 1990 >> North America - East Central NA

这可以说是最快枚举程序,比网上现有的暴力枚举程序要快。

问题链接:UVA725 UVALive5362 Division。基础练习题,用C语言编写程序。

题意简述:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果。如果没有找到则输出“There are no solutions for N.”。这里2<=n<=79。

没有什么好办法,就暴力枚举吧!不过还是要下点功夫,否则10!的计算量是不可想象的。

1.因为n>=2,且abcde=fghij×n,满足abcde>fghij。若a=0,则fghij的最小值为12345,abcde<fghij,矛盾。所以a≠0。

2.因为a≠0,所以12345<=abcde<=98765,01234<=fghij。

3.因为2≤n,且abcde≤98765,那么fghij
= abcde/n,得fghij≤98765/2=49382,所以01234≤fghij≤49382。

4.因为12345≤abcde≤98765,且01234≤fghij≤49382,所以用fghij进行枚举范围比较小。(这是在任意的n的条件下得出的结论)

5.对于给定的n,因为abcde≤98765,那么fghij
= abcde/n,得fghij≤98765/n。结论:01234≤fghij≤98765/n。

AC的C语言程序如下:

/* UVA725 UVALive5362 Division */

#include <stdio.h>
#include <memory.h>

#define DIGITNUM 10

int check(int abcde, int fghij)
{
    int used[DIGITNUM], d;

    memset(used, 0, sizeof(used));

    if(fghij < 10000)
        used[0] = 1;

    while(abcde) {
        d = abcde % 10;
        if(used[d])
            return 0;
        used[d] = 1;

        abcde /= 10;
    }

    while(fghij) {
        d = fghij % 10;
        if(used[d])
            return 0;
        used[d] = 1;

        fghij /= 10;
    }

    return 1;
}

int main(void)
{
    int n, abcde, count, caseflag=0, end, i;

    while(scanf("%d", &n) != EOF && n != 0) {
        if(caseflag)
            printf("\n");
        caseflag = 1;

        count = 0;
        end = 98765 / n;
        for(i=1234; i<=end; i++) {
            abcde = i * n;
            if(abcde >= 12345 && check(abcde, i)) {
                printf("%05d / %05d = %d\n", abcde, i, n);
                count++;
            }
        }
        if(count == 0)
            printf("There are no solutions for %d.\n", n);
    }

    return 0;
}
时间: 2024-10-17 11:19:21

UVA725 UVALive5362 Division的相关文章

UVA-725除法-Division

分析:  枚举0-9的所有排列?没这个必要,只需要枚举fghij就可以计算出abcde(=fghij * n),然后判断是否所有的数字都不相同即可.不仅程序简单,而且枚举量也从10!=3628800降低至不到1万,而且当abcde的位数不等于5的时候,就可以终止枚举了(记住n是大于等于2的哟!) AC代码如下:用时为1573MS. #include<cstdio> #include<cstring> #include<iostream> #include<cmat

uva725 Division

题目描述: abcde / fghij =N a,b···j 为0-9任意一个数,且互相不同 任意给一个n(2<=n<=79),输出满足条件的可能 思路1:10!只有不到400w,直接暴力枚举即可 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[]= {1,0,2,3,4,5,6,7,8

UVA725 Division【枚举】

  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij = N wher

Division UVa725

题意:输入一个n(2<=n<=79) 找出是否从存在abcde/fghij=n的表达式 直接枚举,对于每一个fghij,判断abcde,所有数字不相等就可以(每个数字都出现),但要注意枚举的范围,还有格式,格式真的头疼. 代码如下 #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define INF 0x7fffffffffffffff typedef long long ll; const double PI=3.14159265

light oj 1214 - Large Division

1214 - Large Division   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if

fzuoj1607Greedy division

题目链接: 点我点我 题目:  Problem 1607 Greedy division Accept: 436    Submit: 1590 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunate

HDU 3480 Division(斜率优化+二维DP)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 3984    Accepted Submission(s): 1527 Problem Description Little D is really interested in the theorem of sets recently. There’s a pro

BZOJ 1385: [Baltic2000]Division expression

题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec  Memory Limit: 64 MB Description 除法表达式有如下的形式: X1/X2/X3.../Xk 其中Xi是正整数且Xi<=1000000000(1<=i<=k,K<=10000) 除法表达式应当按照从左到右的顺序求,例如表达式1/2/1/2的值为1/4.但可以在表达式中国入括号来改变计算顺序,例如(1/2)/(1/2)的值为1.现给出一个除

枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

1 //Uva725 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 using namespace std; 7 8 void evalu(int n) 9 { 10 const int maxn = 1024 + 10; 11 char num[10]; //将数字用字符保存 12 int flag[10]; //判断每个数,是否重