zoukankan      html  css  js  c++  java
  • Project Euler problem 61

    题意很明了。


    然后我大概的做法就是暴搜了

    先把每个几边形数中四位数的处理出来。

    然后我就DFS回溯着找就行了。

    比较简单吧。


    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <vector>
    using namespace std;
    vector<int>g[7], v[7][10000];
    int vis[7];
    void get_three()
    {
        for(int i = 1; ; i++)
        {
            int x = i * (i + 1) / 2;
            if(x >= 10000) break;
            if(x >= 1000) g[0].push_back(x);
        }
    }
    void get_four()
    {
        for(int i = 1; ; i++)
        {
            int x = i * i;
            if(x >= 10000) break;
            if(x >= 1000) g[1].push_back(x);
        }
    }
    void get_five()
    {
        for(int i = 1; ; i++)
        {
            int x = i * (3 * i - 1) / 2;
            if(x >= 10000) break;
            if(x >= 1000) g[2].push_back(x);
        }
    }
    void get_six()
    {
        for(int i = 1; ; i++)
        {
            int x = i * (2 * i - 1);
            if(x >= 10000) break;
            if(x >= 1000) g[3].push_back(x);
        }
    }
    void get_seven()
    {
        for(int i = 1; ; i++)
        {
            int x = i * (i * 5 - 3) / 2;
            if(x >= 10000) break;
            if(x >= 1000) g[4].push_back(x);
        }
    }
    void get_eight()
    {
        for(int i = 1; ; i++)
        {
            int x = i * (3 * i - 2);
            if(x >= 10000) break;
            if(x >= 1000) g[5].push_back(x);
        }
    }
    int a[7];
    int ans;
    int flag;
    void dfs(int deep)
    {
        if(flag) return;
        if(deep == 6)
        {
            if(a[5] % 100 == a[0] / 100)
            {
                flag = 1;
                printf("%d
    ", a[0] + a[1] + a[2] + a[3] + a[4] + a[5]);
                return;
            }
        }
        for(int i = 0; i < 6; i++)
        {
            if(!vis[i])
            {
                for(int j = 0; j < g[i].size(); j++)
                {
                    int y = g[i][j];
                    if(!deep || y / 100 == a[deep - 1] % 100)
                    {
                        vis[i] = 1;
                        a[deep] = y;
                        dfs(deep + 1);
                        vis[i] = 0;
                    }
                }
            }
        }
    }
    int main()
    {
        get_three();
        get_four();
        get_five();
        get_six();
        get_seven();
        get_eight();
        dfs(0);
        return 0;
    }
    


  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/pangblog/p/3348030.html
Copyright © 2011-2022 走看看