数论vs图论

本文介绍了Mayuyu解决的一个数论问题,通过构建图论模型来解决给定正整数的特殊条件。题目要求找出所有因子,并以因子间的关系建立图,然后进行深度遍历计算每个节点的最大深度值。最后,计算所有节点序号与其最大深度值乘积的和。解决方案采用了链式前向星处理方法。

最近Mayuyu遇到个神奇的数论题目,Mayuyu能做出来真的不容易啊,描述如下。

 

题目:给定一个正整数,满足条件,以为根节点进行扩展,对于每一个节点,它只能到达能整除

     它的节点,如果存在节点,使得成立,则必定会经过点,对于每一个节

     点,有一个值,这个值等于这个节点的最大深度,最后求输出每个节点的序号乘对应值的和。

 

分析:对于一个数,它只能到达它的所有因子,所以第一步就是找出的所有因子,然后就是如果两个因子出现一

     个能整除另一个的情况,就连一条边。最后就构建了一个图,在这个图中,我们从开始进行深度遍历,纪录

     到达点的最大深度值,最后加起来就可以了。这里采用链式前向星处理。

 

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;
const int N = 5005;
typedef long long LL;

int ct,cnt;
LL fac[N];
int head[N];

struct Edge
{
    int to;
    int next;
    int w;
};

Edge edge[N*N];

void Init()
{
    cnt = 0;
    memset(edge,0,sizeof(head));
    memset(head,-1,sizeof(head));
}

void add(int u,int v,int w)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    edge[cnt].w = w;
    head[u] = cnt++;
}

void Find(LL n)
{
    ct = 0;
    LL t = (LL)sqrt(1.0*n);
    for(int i=1; i<=t; i++)
    {
        if(n % i == 0)
        {
            if(i * i == n) fac[ct++] = i;
            else
            {
                fac[ct++] = i;
                fac[ct++] = n / i;
            }
        }
    }
}

void dfs(int u,int dept)
{
    for(int i=head[u]; ~i; i=edge[i].next)
    {
        int t = edge[i].to;
        edge[t].w = max(edge[t].w, dept + 1);
        dfs(t,dept+1);
    }
}

int main()
{
    LL n;
    while(cin>>n)
    {
        Find(n);
        Init();
        sort(fac,fac+ct);
        for(int i=0; i<ct; i++)
        {
            for(int j=i+1; j<ct; j++)
                if(fac[j] % fac[i] == 0)
                    add(j,i,0);
        }
        dfs(ct-1,1);
        LL ans = 0;
        for(int i=0; i<ct; i++)
            ans += edge[i].w * fac[i];
        cout<<ans + n<<endl;
    }
    return 0;
}


 

 

Graph Theory Authors: Adrian Bondy, U.S.R Murty Publisher: Springer; 3rd Corrected Printing edition (Aug 14 2008) ISBN-10: 1846289696 ISBN-13: 978-1846289699 = Product Description = The primary aim of this book is to present a coherent introduction to graph theory, suitable as a textbook for advanced undergraduate and beginning graduate students in mathematics and computer science. It provides a systematic treatment of the theory of graphs without sacrificing its intuitive and aesthetic appeal. Commonly used proof techniques are described and illustrated. The book also serves as an introduction to research in graph theory. = Table of Contents = Graphs - Subgraphs - Connected Graphs - Trees - Separable and Nonseparable Graphs - Tree-Search Algorithms - Flows in Networks - Complexity of Algorithms - Connectivity - Planar Graphs - The Four-Colour Problem - Stable Sets and Cliques - The Probabilistic Method - Vertex Colourings - Colourings of Maps - Matchings - Edge Colourings - Hamilton Cycles - Coverings and Packings in Directed Graphs - Electrical Networks - Integer Flows and Coverings - Unsolved Problems - References - Glossary - Index = Reviews = For more than three decades, the authors' Graph Theory with Applications (1976) has served as perhaps the classic introduction to graph theory. With happy shock, the reader learns that Bondy and Murty have updated the book, doubling its size. Three decades would count as a long time in the life of any active scientific pursuit, but the original year of publication saw the solution to the four-color conjecture that catalyzed a vast revitalization of graph theory. Graph theory, moreover, now has intimate interactions with computer science, practical and theoretical: three decades ago, computer networks barely existed and the founding papers of complexity theory had just appeared. Connections between graph theory and probability have also undergone a revolution. In short, the passage of time has transformed this subject in these and other ways, and the authors have transformed their book accordingly. They do, by choice, largely omit the theory of graph minors (developed by Paul Seymour and Neil Robertson and followers), which certainly now deserves a monographic treatment of its own. Summing up: Recommended. Lower-division undergraduate through professional collections. CHOICE This book is a follow-on to the authors' 1976 text, Graphs with Applications. What began as a revision has evolved into a modern, first-class, graduate-level textbook reflecting changes in the discipline over the past thirty years... This text hits the mark by appearing in Springer’s Graduate Texts in Mathematics series, as it is a very rigorous treatment, compactly presented, with an assumption of a very complete undergraduate preparation in all of the standard topics. While the book could ably serve as a reference for many of the most important topics in graph theory, it fulfills the promise of being an effective textbook. The plentiful exercises in each subsection are divided into two groups, with the second group deemed "more challenging". Any exercises necessary for a complete understanding of the text have also been marked as such. There is plenty here to keep a graduate student busy, and any student would learn much in tackling a selection of the exercises... Not only is the content of this book exceptional, so too is its production. The high quality of its manufacture, the crisp and detailed illustrations, and the uncluttered design complement the attention to the typography and layout. Even in simple black and white with line art, it is a beautiful book. SIAM Book Reviews "A text which is designed to be usable both for a basic graph theory course … but also to be usable as an introduction to research in graph theory, by including more advanced topics in each chapter. There are a large number of exercises in the book … . The text contains drawings of many standard interesting graphs, which are listed at the end." (David B. Penman, Zentralblatt MATH, Vol. 1134 (12), 2008) MathSciNet Reviews "The present volume is intended to serve as a text for "advanced undergraduate and beginning graduate students in mathematics and computer science" (p. viii). It is well suited for this purpose. The writing is fully accessible to the stated groups of students, and indeed is not merely readable but is engaging… Even a complete listing of the chapters does not fully convey the breadth of this book… For researchers in graph theory, this book offers features which parallel the first Bondy and Murty book: it provides well-chosen terminology and notation, a multitude of especially interesting graphs, and a substantial unsolved problems section…One-hundred unsolved problems are listed in Appendix A, a treasure trove of problems worthy of study… (In short) this rewrite of a classic in graph theory stands a good chance of becoming a classic itself." "The present volume is intended to serve as a text for ‘advanced undergraduate and beginning graduate students in mathematics and computer science’ … . The writing is fully accessible to the stated groups of students, and indeed is not merely readable but is engaging. The book has many exercise sets, each containing problems … ." (Arthur M. Hobbs, Mathematical Reviews, Issue 2009 C) "A couple of fantastic features: Proof techniques: I love these nutshelled essences highlighted in bordered frames. They look like pictures on the wall and grab the view of the reader. Exercises: Their style, depth and logic remind me of Lovász’ classical exercise book. Also the fact that the name of the author is bracketed after the exercise…Figures: Extremely precise and high-tech…The book contains very recent results and ideas. It is clearly an up-to-date collection of fundamental results of graph theory…All-in-all, it is a marvelous book." (János Barát, Acta Scientiarum Mathematicarum, Vol. 75, 2009)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值