HDU 1223 打表 + 大数

http://acm.hdu.edu.cn/showproblem.php?pid=1223

一般遇到这些题,我都是暴力输出前几项,找规律。未果。

然后输出n = 1时候,以A开始,有多少个答案,

n = 2的时候,A开始,B开始,有多少个答案。然后发现了规律。大数之

const int maxn = 50 + 20;
struct node {
    int val;
    int id;
    bool operator < (const struct node & rhs) const {
        if (val != rhs.val) return val < rhs.val;
        else return id < rhs.id;
    }
}a[maxn];
int n;
int f[maxn];
int lena;
map<string, bool>mp;
int aa;
void dfs(int cur) {
    if (cur == n + 1) {
        for (int i = 1; i <= n; ++i) {
            a[i].val = f[i];
            a[i].id = i;
        }
        sort(a + 1, a + 1 + n);
        string s;
        s += ‘A‘ + a[1].id - 1;
        for (int i = 2; i <= n; ++i) {
            if (a[i].val == a[i - 1].val) {
                s += "=";
            } else s += "<";
            s += ‘A‘ + a[i].id - 1;
        }
        if (mp[s]) return;
        if (s[0] == ‘C‘) {
            cout << s << endl;
            aa++;
        }
        mp[s] = true;
        return;
    }
    for (int i = 1; i <= n; ++i) {
        f[cur] = i;
        dfs(cur + 1);
    }
}
void work() {
    n = 6;
    dfs(1);
    cout << mp.size() << endl;
    cout << aa << endl;
}

dfs打表

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <iomanip>
const int maxn = 500 + 20;
void bigadd (char str1[],char str2[],char str3[]) {    //str1 + str2 = str3
    int len1=strlen(str1+1),len2=strlen(str2+1);
    char b[maxn]= {0};
    int i=len1,j=len2;
    int h=1;
    while (i>=1 && j>=1)  b[h++] = str1[i--]-‘0‘ + str2[j--]-‘0‘;
    while (i>=1)         b[h++] = str1[i--]-‘0‘;
    while (j>=1)         b[h++] = str2[j--]-‘0‘;
    for (int i=1; i<h; i++) { //h是理论越界的
        if (b[i] >= 10) {
            b[i+1]++;
            b[i] -= 10;
        }
    }
    if (!b[h]) --h;//没有进位到越界位。
    int t=h;
    for (int i=1; i<=h; i++)  str3[t--]=b[i]+‘0‘;
    str3[h+1]=‘\0‘; //一定要手动添加结束符,不然会GG
    return ;
}
char a[maxn][maxn][maxn];
char last[maxn];
char t[maxn];
void init() {
//    a[1][1] = 1.0;
    strcpy(a[1][1] + 1, "1");
//    long double last = 1.0;
    strcpy(last + 1, "1");
    for (int j = 2; j <= 52; ++j) {
//        a[j][j] = last;
        strcpy(a[j][j] + 1, last + 1);
//        printf("%s\n", a[j][j] + 1);
        for (int i = j - 1; i >= 1; --i) {
//            a[i][j] = a[i + 1][j] + a[i][j - 1];
            bigadd(a[i + 1][j], a[i][j - 1], a[i][j]);
//            printf("%s\n", a[i][j] + 1);
        }
//        last = 0;
        memset(t, 0, sizeof t);
        for (int k = j; k >= 1; --k) {
//            last += a[k][j];
            bigadd(a[k][j], t, last);
            strcpy(t + 1, last + 1);
        }
    }
}
void work() {
    int n;
    scanf("%d", &n);
    printf("%s\n", a[n + 1][n + 1] + 1);
}
int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    init();
    int t;
    scanf("%d", &t);
    while (t--) work();
    return 0;
}

时间: 2024-08-06 03:45:46

HDU 1223 打表 + 大数的相关文章

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆

hdu 4951 乘法表

http://acm.hdu.edu.cn/showproblem.php?pid=4951 依照我原先的想法是 先找出ans[0] 和ans[1]来,这个好找吧,要是有一行全部是相等的数组成那这行的序号就是ans[0], 要是有一[i,j],i*j=ans[0] i ,那么ans[j]=1,  找到了ans[0]和ans[1] ,然后根据已知的遍历推算出其他的结果. 1 #include<iostream> 2 #include<cstdio> 3 #include<map

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDU 1047 Integer Inquiry 大数相加 string解法

本题就是大数相加,题目都不用看了. 不过注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h

HDU 5047 Sawtooth(大数优化+递推公式)

http://acm.hdu.edu.cn/showproblem.php?pid=5047 题目大意: 给n条样子像“m”的折线,求它们能把二维平面分成的面最多是多少. 解题思路: 我们发现直线1条:2平面:2直线:4平面:3直线:7平面......因为第n条直线要与前面n-1条直线都相交,才能使分的平面最多,则添加第n条直线,平面增加n个: 所以公式是面F = 2 + 2 + 3 + ......+ n = (1+n)*n/2 + 1 因为题目的是“M”的折线,一个“M”有4条线将平面分成2

hdu 1061打表

背景:某水题.第一次感觉自己写的代码最优化了,毕竟hdu上0ms 0k. 学习:1.一看n可达1e10,显然不可硬来,乘法个位数,只和每次相乘的个位数有关,故直接把0~9打表. #include<stdio.h> //¶Ô0~9£¬Ã¿¸öÊý×ֵĿÉÄܽá¹ûд³ö£¬µÚһλ±íʾӵÓнá¹ûÖÖÊý¡£ int str[10][5]={{1,0},{1,1},{4,2,4,8,6,},{4,3,9,7,1},{2,4,6},{1,5},{1,6},{4,7,9,3,1}

hdu 1212 Big Number(大数取模)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7083    Accepted Submission(s): 4884 Problem Description As we know, Big Number is

Buy the Ticket HDU 1133 递推+大数

题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目大意: 有m+n个人去买电影票,每张电影票50元,  m个人是只有50元一张的,  n个人是只有100元一张的, 电影院自己本身是没有零钱的. 那么要收到100元的钱必须找人家50, 那么再次之前就必须 收到一个50元的, 问你有多少种不同的排列方式. (注意: 这里每个人都看成了不同的元素) 题目分析: 我们要是能找人家钱首先必须要有 m >= n 我们dp[m][n] 再加一个人 只