鸽巢定理和容斥定理 (POJ 2356 、POJ 3370 + HDU 1796)

容斥原理、鸽巢原理快速入门 一、容斥定理基本描述在计数时,必须不重不漏。为了使得重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后把计数时重复计算的数目排斥出去,使得计算的结果既无重复也无遗漏,这种计数的方法称为容斥原理。公式表述公式的解释: 目的是求解m个集合的并集,首先将m个集合相加,减去集合间两两相交的部分,加上三三相交的部分,再 阅读详情

 

真的好久没写博客了

之前是考试周就没怎么做题

后来考试后回家 去厦门玩耍了三四天

19号回学校 20号开始集训

emmmm 结束碎碎念 开始进入正题吧~

 

目录

鸽巢定理:

POJ  2356

POJ  3370 && HDU 1808

容斥定理:

HDU 1796


鸽巢定理:

也叫 抽屉定理, 抽屉原理的一般含义为:“如果每个抽屉代表一个集合,每一个苹果就可以代表一个元素,假如有n+1个元素放到n个集合中去,其中必定有一个集合里至少有两个元素。”

第一抽屉原理:

原理1: 把多于n+1个的物体放到n个抽屉里,则至少有一个抽屉里的东西不少于两件。

原理2 :把多于mn(m乘n)+1(n不为0)个的物体放到n个抽屉里,则至少有一个抽屉里有不少于(m+1)的物体。

原理3 :把无穷多件物体放入n个抽屉,则至少有一个抽屉里 有无穷个物体。

第二抽屉原理:

把(mn-1)个物体放入n个抽屉中,其中必有一个抽屉中至多有(m—1)个物体

(例如,将3×5-1=14个物体放入5个抽屉中,则必定有一个抽屉中的物体数少于等于3-1=2)。

 

POJ   2356

                                                       Find a multiple

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9020 Accepted: 3870 Special Judge

Description

The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).

Input

The first line of the input contains the single number N. Each of next N lines contains one number from the given set.

Output

In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order. 

If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.

Sample Input

5
1
2
3
4
1

Sample Output

2
2
3

题目大意:

给出N以及N个数,从这N个数中任意选择1个或多个数,使得其和是N的倍数。

其实可以用暴力求解 但是很费时间 所以就用抽屉原理来做

PS:也是看了别人的博客才懂的 但是写了详细的注解

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
int sum[10005],a[10005],mod[10005];
using namespace std;
int main()
{
    int t;
    while (~scanf("%d",&t))
    {
        memset(mod,-1,sizeof (mod));//每执行一次样例 对mod数组重置-1
        for (int i=0; i<t; i++)
        {
            scanf("%d",&a[i]);
        }
        for (int i=0; i<t; i++)
        {
            sum[i+1]=sum[i]+a[i];
            if (sum[i+1]%t==0)//若某个sum值是t的倍数 直接输出a[0]~a[i]
            {
                int j;
                printf("%d\n",i+1);//个数
                for (j=0; j<=i; j++)
                    printf("%d\n",a[j]);
                break;
            }
            if (mod[sum[i+1]%t]!=-1)//若出现有相同的余数的值 则依次输出
            {
                int j;
                printf("%d\n",i-mod[sum[i+1]%t]);//个数
                for (j=mod[sum[i+1]%t]+1; j<=i; j++)
                    printf("%d\n",a[j]);
                break;
            }
            mod[sum[i+1]%t]=i;//将相应余数存到mod中
        }
    }
    return 0;
}

 

加上一题 (类似的 也是求前缀和 然后得到几个数组成的某个数的倍数)

POJ 3370 && HDU 1808

Halloween treats

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1221    Accepted Submission(s): 479
Special Judge

 

Problem Description

Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoid conflicts, the children have decided they will put all sweets together and then divide them evenly among themselves. From last year's experience of Halloween they know how many sweets they get from each neighbour. Since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbours to visit, so that in sharing every child receives the same number of sweets. They will not be satisfied if they have any sweets left which cannot be divided. 

Your job is to help the children and present a solution. 

Input

The input contains several test cases. 
The first line of each test case contains two integers c and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains n space separated integers a1 , ... , an (1 ≤ ai ≤ 100000 ), where ai represents the number of sweets the children get if they visit neighbour i. 

The last test case is followed by two zeros. 

Output

For each test case output one line with the indices of the neighbours the children should select (here, index i corresponds to neighbour i who gives a total number of aisweets). If there is no solution where each child gets at least one sweet, print "no sweets" instead. Note that if there are several solutions where each child gets at least one sweet, you may print any of them.

Sample Input

4 5 1 2 3 7 5 3 6 7 11 2 5 13 17 0 0

Sample Output

3 5 2 3 4

 

题目大意:

给你两个整数C和N,再给你N个正数的序列,从中找到若干数,使得其和刚好是 C的倍数。输出这些数的序号。

PS:

样例给出的结果 只是其中的一个解 (存在多解)

要注意的是 要求输出的是 序号 而不是 数组内的数字 (与上一题有点不同)

题目中说 C < N 即人数小于邻居的数量,所以必存在一种结果

#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdio.h>
int a[100005];
int mod[100005];
//标记余数出现的位置
using namespace std;
int main()
{
   // freopen("in.txt","r",stdin);
    int n,m;int sum;
    while (~scanf("%d%d",&n,&m)&&n)
    {
        memset(a,0,sizeof a);
        memset(mod,-1,sizeof (mod));
        sum=0;
        for (int i=1; i<=m; i++)
        {
            scanf("%d",&a[i]);
            a[i]=a[i]%n;
        }
        for (int i=1;i<=m;i++)
        {
            sum=(sum+a[i])%n;
            if (!sum)
            {
                printf("%d",1);
                for (int j=2;j<=i;j++)
                    printf(" %d",j);
                printf("\n");
                break;
            }
            else if (mod[sum]!=-1)
            {
                printf("%d",mod[sum]+1);
                for (int j=mod[sum]+2;j<=i;j++)
                    printf(" %d",j);
                printf("\n");
                break;
            }
            mod[sum]=i;
        }
    }
    return 0;
}

 

 

容斥定理:

        在计数时,必须注意没有重复,没有遗漏。为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复,这种计数的方法称为容斥原理。

       如果被计数的事物有A、B、C三类,那么,A类和B类和C类元素个数总和= A类元素个数+ B类元素个数+C类元素个数—既是A类又是B类的元素个数—既是A类又是C类的元素个数—既是B类又是C类的元素个数+既是A类又是B类而且是C类的元素个数。(A∪B∪C = A+B+C - A∩B - B∩C - C∩A + A∩B∩C)

以下是从百度百科偷来的公式图:

HDU 1796

How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10406    Accepted Submission(s): 3109

Problem Description

  Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.

Input

  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.

Output

  For each case, output the number.

Sample Input

12 2

 2 3

Sample Output

7

题目大意:

给出N和M个数,求不超过N 且可以被M个数中任意一个整除的数有多少个。

先求出能被单个因子整除的数目 之后 求出被多个因子整除的数目 ——> 奇加偶减

 

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define ll long long
ll a[20];
using namespace std;
ll gcd(ll a,ll b)//计算最大公约数
{
    return b?gcd(b,a%b):a;
}
int main()
{
    ll n,m;
    while (~scanf("%lld%lld",&n,&m))
    {
        n=n-1;
        int h=1;
        for (int i=1; i<=m; i++)
        {
            scanf("%lld",&a[h]);
            if (a[h]!=0)h++;
        }
        m=h-1;
        ll ans=0,k,id;
        for (int i=1; i<(1<<m); i++)// << 是移位运算 二进制左移一位 相当于i<2^m
        {
            k=1;
            id=0;
            for (int j=0; j<m; j++)
                if (i&(1<<j))//二进制控制因子
                {
                    id++;
                    k=(a[j+1]/gcd(a[j+1],k)*k);
                }
            if (id&1)//且运算 判断id的末位是否为1
                //相当于判断奇偶 id%2==1 (奇加偶减)
                ans+=n/k;
            else ans-=n/k;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

上面的代码运用到了位运算 对此不了解的可以自行百度

或者自己走一遍程序 走一遍就能懂了( ̄m ̄)

 

欧克 七月的第一篇也是最后一篇~

明天也要加油鸭!!!

算法之组合数学及其算法篇(二) ----- 鸽巢原理 鸽巢原理 前言 鸽巢原理 运用1 运用二 运用三 鸽巢原理的推广 推论 运用一 运用二 鸽巢原理在几何上的作用 鸽巢原理对于数学的证明 阅读详情

相关推荐

【离散数学】集合论 第四章 函数与集合(3) 鸽巢原理

本文属于「离散数学」系列文章之一。这一系列着重于离散数学的学习和应用。由于内容随时可能发生更新变动,欢迎关注和收藏离散数学系列文章汇总目录一文以作备忘。此外,在本系列学习文章中,为了透彻理解数学知识,本人参考了诸多博客、教程、文档、书籍等资料。以下是本文的不完全参考目录,在后续学习中还会逐渐补充: (国外经典教材)离散数学及其应用 第七版 Discrete Mathematics and Its Applications 7th ,作者是 Kenneth H.Rosen ,袁崇义译,机械工业出版社 离散.

memcpy0的博客 6273

鸽巢原理

鸽巢原理:如果鸽子数比鸽巢数多,那么一定有一个鸽巢里至少有两只鸽子 定理1:k+1或更多个物体放入k个盒子,一定至少有一个盒子有2个或更多的物体 应用1:对每个整数n,存在一个数是n的倍数,且它的十进制表示只包含0和1 证:n+1个数,1,11,111,...,111……111(n+1个1)。这些数被n整除,得到n+1个余数,由于,一个数整除n只能得到0~n-1的n个数,所以

ki187的专栏 1160

解决Keil uVision许可证管理中Unknown Product错误

嵌入式开发中,IDE工具的许可证管理是确保开发环境正常运行的关键环节。Keil uVision作为ARM架构的主流开发工具,其许可证系统采用加密验证机制,通过产品版本识别实现授权控制。当出现Unknown Product错误时,通常源于版本兼容性问题或区域发行版差异,这直接影响开发工具链的可用性。特别是在维护历史项目或使用中国特供版时,开发者常会遇到此类问题。通过分析许可证类型、调整安装配置或建立虚拟化开发环境等工程实践方案,可以有效解决这类授权验证故障,保障嵌入式项目的持续集成与交付效率。

weixin_33743248的博客 369

(Ramsey定理 )(鸽巢定理)( 组合数学)

组合数学 鸽巢原理: 典型题目:吃糖果 很奇妙的思路找糖果最多的作为隔板,假设有N个隔板,剩余的糖果S必须有S>=N才有唯一解(可以分类讨论一下很有趣,在S<N时,无解) 鸽巢原理是Ramsey定理的一个特例: ...

m0_51506743的博客 4439

Remote ID的调制解调FPGA实现

Remote ID源于航空安全需求,例如在无人机密集区域实现设备跟踪和防碰撞。它类似于航空领域的ADS-B系统,但针对低空飞行器。Remote ID系统通常工作在特定频段(如2.4 GHz或5.8 GHz),通过无线链路广播数据包,包含设备ID、经纬度、高度和速度等信息。调制解调过程确保这些数据在噪声环境中可靠传输。例如,标准如ASTM F3411定义了Remote ID的协议框架,其中调制解调是关键物理层组件。

yulongfenghu的博客 1513

手把手教你用AirSim生成无人机训练数据集:RGB/深度图/分割图全攻略

本文详细介绍了如何使用AirSim仿真插件在UE4环境中生成无人机训练数据集,涵盖RGB、深度图和分割图等多种模态数据的采集与处理。通过实战指南,读者可以掌握环境配置、多模态数据采集、自动化流程设计及数据后处理等关键技术,为无人机视觉算法开发提供高质量训练数据。

weixin_29008805的博客 458

鸽巢原理(Pigeonhole Principle)

鸽巢原理(Pigeonhole Principle)的基本定理: If n + 1 objects are distributed into n boxes, then at least one box contains two or more of the objects. 一个简单的例子:13个人里至少有两个人的生日是同一月的。 鸽巢原理的相关原理: If n objects are p

theArcticOcean 6817

手把手教你用Verilog实现AT24C64的I2C读写(附完整仿真流程)

本文详细介绍了如何使用Verilog HDL设计AT24C64 EEPROM的I2C控制器,并配合ModelSim完成完整的仿真验证流程。内容涵盖I2C协议解析、Verilog状态机设计、AT24C64读写操作实现,以及仿真波形分析和调试技巧,为FPGA开发者提供了一套完整的EEPROM控制解决方案。

3c4x5z6v7b的博客 853

鸽巢原理详解(口水化解释)

鸽巢原理详解前言:鸽巢原理简单的例子一. 任何367个人里面至少有两人生日相同。二.任意11个整数中,至少有2个整数之差是10的倍数。需要动点脑子的例子一. 分配职工训练时间问题二. 自行车问题三.在世界上任意6个人的集体中,要么有3个人互相认识,要么有3个人互不认识。 前言: 鸽巢原理是离散数学学习种一大难点,主要在于如何去构造鸽子和巢的抽象化,本文是整理了一些笔者学习中遇到的典型例题,加了一些笔者自己的思路,希望能帮助学习离散数学的同学。 PS:这里的大部分例题都是来源于我的课本或者老师课堂讲的例题。

zx1772874955的博客 1万+

【BLE】SIG-蓝牙技术联盟:会员特权与BQB认证全解析

本文全面解析了蓝牙技术联盟(SIG)的核心角色与BQB认证流程。详细阐述了SIG会员(应用会员与联盟会员)的不同特权、成本结构及法律协议,并提供了从准备到完成的BQB认证实战指南,帮助企业合规使用蓝牙技术并规划长期商业价值。

game4的博客 615

Zemax与Matlab交互:双胶合优化详细流程

Zemax与Matlab交互:双胶合优化详细流程

weixin_72050316的博客 633

分布式一致性算法的重要原理:鸽巢原理

在分布式BASE理论:数据一致性模型有哪些?中,我们谈到了BASE理论的最终一致性,以及简单介绍了数据一致性模型,但我们都是站在一个使用者的角度,在发出数据更新的请求给分布式系统之后,观察返回的数据是否更新。为了更好使用、理解分布式系统,不妨换个视角,观察在服务端是怎么实现一致性的。 因为分布式算法还是比较难的,柿子先挑软的捏,笔者在这篇文章先对分布式中一致性算法的基石原理——鸽巢原理进行介绍: ...

NYfor2020的博客 2323

西门子S7-1200PLC控制步进电机的组态编程和具体步骤

西门子S7-1200PLC控制步进电机的组态编程和具体步骤 硬件准备: 1) 一个S7-1214C DC/DC/DC,订货号:6ES7 214-1AG40-0XB0 2) 一个86系的步进电机+相应的驱动器 3) 一个20A的西门子24V电源(步进驱动器及plc电源) 4) Portal V15版本 组态及编程: 1) 创建一个验证项目,添加cpu( S7-1214C),双击“工艺对象”,选择“运动控制”的TO_PositioningAxis V6.0版本,如下图所示: 2) “确定”后,系统自动生成名

Robot_PLC_HMI_Automation 1万+

鸽巢原理分析、实用技巧、部分定理证明以及组合数学学习心路历程

鸽巢原理 天上有十个鸽子,这十个鸽子要飞到九个鸽巢里,无论怎样飞,我们会发现至少会有一 个鸽巢里面放两个鸽子,这一现象就是我们所说的“鸽巢原理”。鸽巢定理由狄里克利于1834 年提出,当时命名为 Schubfachprinzip(drawer principle)。 鸽巢原理的一般含义为:“如果每个鸽巢代表一个集合,每一个鸽子就可以代表一个元素,假如有 n 或多于n 个元素放到n-1 个...

qq_35293120的博客 9376

硬件知识点-----SPI串联电阻、振铃、过冲

电阻取值,没有公式,一被传输线的特征阻抗为50欧姆左右,而TTL电路输出电阻大桃为13欧姆左右,在源端串一个33姆,大致和50相当,这样就可以柳制从终端反射回来的倍号,防止再次反射。传输时间tp=L/V,其中L是导线长度,V是电磁波在电路板中的传输速度V=C/sqrDk,其中Dk是光速衰减因子,对于电路板而言是铜线,Dk=4电磁波在电路板中的速度都是慢于光速的。实际上串联多大的电阻与传输线的阻抗有关,为了保证阻抗的连续性,如果传输线的特性阻抗 Z0与驱动源的输出阻抗 Zd不匹配,会引发信号的反射。

贾saisai的博客 2749

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

抽屉原理(鸽巢原理)和拉姆齐定理

1、抽屉原理:把十个苹果放到九个抽屉,那么至少有一个抽屉里的苹果不少于两个。 2、拉姆齐定理:在总人数大于等于6的时候,一定存在这样的三个人,他们相互认识,或者相互不认识。 其中,证明如下: 我们不妨假设有6个人,设有A、B、C、D、E、F。 用红线连接两个人,表示他们认识,用蓝线连接两个人,表示他们不认识。 根据鸽巢原理可以知道,A至少认识三个人,或者至少不认识三个人。 不妨设A认识三个人B、C、D,也就是AB、AC、AD是红线。 1、这样的话,如果BC、BD、CD其中一条也是红线,那么就是组成一个红色三

菜鸡的学习记录 1875

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

组合数学——鸽巢原理

假设n个抽屉至多有1件物品,那么至多有n件物品,与有n+1件物品的条件矛盾,故鸽巢原理成立。个“物品”),根据鸽巢原理,至少有两个数属于同一余数类,即这两个数的余数相同。证明任意选取的 n + 1个整数中,必存在两个数,它们的差为n的倍数。如果把n+1件物品放进n个抽屉,必定存在至少一个抽屉里有超过两件物品。个整数中,必存在两个数,它们的差为。件物品,与题设矛盾,故原定理成立。个物品放进n个抽屉,则第。件物品,则n个抽屉最多有。

k42946的博客 1782

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【计算复杂性理论】证明复杂性(九):命题鸽巢原理的指数级归结下界——更简短的证明

定义宽子句为含有至少n21010n2​个文字(即变量)的子句,也就是包含了至少总变量个数的(大约)110101​的子句。例如,当n5n=5n5时,子句P12∨P13∨P14P12​∨P13​∨P14​就是包含了3521031052​个变量的宽子句。为什么选择分母为101010呢?我们待会会看到其原因。定理对于充分大的nnn,任何对PHPnn−1PHPnn−1​的归结证明的大小都至少为2。

qaqwqaqwq的博客 1125

【离散数学中的数据结构与算法】九 鸽巢原理

鸽巢原理是非常著名的原理,生活正用的也很多。 文章目录1 简单鸽巢原理的应用2 定理(一般性鸽巢原理)2.1 应用3 总结 1 简单鸽巢原理的应用 定理(鸽巢原理) 若有 n 个鸽巢, n+1 个鸽子,则至少有一个巢内有至少两个鸽子。 例1 假设在一个盒子里面有10双黑色袜子、 12双蓝色袜子和8双红色袜子。那么拿出4只袜子一定可以保证有同色的两只。 每种颜色作为抽屉 拿出的袜子数目作...

厚积薄发 2470

抽屉原理(鸽巢原理)与Ramsey定理

直观描述 桌上有十个苹果,要把这十个苹果放到九个抽屉里,无论怎样放,至少会有一个抽屉里面至少放两个苹果。 如果有五个鸽子笼,养鸽人养了6只鸽子,那么当鸽子飞回笼中后,至少有一个笼子中装有2只鸽子。 抽屉原理有时也被称为鸽巢原理。它是组合数学中一个重要的原理。 内容 ...

用炮弹打苍蝇 2963

一些利用鸽巢原理的优雅证明

鸽巢原理(抽屉原理)及其一般性定理不难证明。这里主要关注利用鸽巢原理的一些优美的证明。

lyh20021209的博客 476

组合数学知识总结(排列组合+母函数+容斥鸽巢原理+FFT和FWT)

这篇文章算是前一段时间组合数学学习的总结吧,总算是告一段落了。 目录 序章 知识点 排列 组合 常用公式和定理 常见数列及其性质 母函数 FFT&&FWT 经典习题 序章 其实本来自己对这些知识已经总结了一部分(手写的),不过发现网上也都有,写的也都挺全面的,就懒省事搬了网上的一部分知识点的总结,当然对于一名Acmer来说只知道知识...

mumei的博客 4489

【组合数学】鸽巢原理 ( 鸽巢原理简单形式 | 鸽巢原理简单形式示例 1、2、3 )

一、鸽巢原理简单形式 、 二、鸽巢原理简单形式示例 2 、 三、鸽巢原理简单形式示例 3 、 四、鸽巢原理简单形式示例 4 、

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 7857

鸽巢原理(简单形式)

文章目录1 简介2 鸽巢原理(简单形式)应用1应用2应用3应用4应用53 参考资料 1 简介   鸽巢原理有许多名字,最普通的名字叫鸽巢原理,其他名字有狄利克雷(Dirichlet)抽屉原理、鞋盒(shoebox)原理。对鸽巢原理粗略地描述就是如果有许多鸽子飞进不够多的鸽巢内,那么至少要有一个鸽巢要被两个或多个鸽子占据。 2 鸽巢原理(简单形式)   定理:如果要把n+1n+1n+1个物体放进nnn个盒子,那么至少有一个盒子包含两个或更多的物体。   可以说这个定理是相当显然的,如果nnn个盒子中的每一个都

qq_39378221的博客 3789

容斥定理+鸽巢原理

目录 一、容斥定理 二、鸽巢原理(抽屉原理)   一、容斥定理 【原理】 容斥原理是一种重要的组合数学方法,可以让你求解任意大小的集合,或者计算复合事件的概率。要计算几个集合并集的大小,我们要先将所有单个集合的大小计算出来,然后减去所有两个集合相交的部分,再加回所有三个集合相交的部分,再减去所有四个集合相交的部分,依此类推,一直计算到所有集合相交的部分。 【公式】 即奇...

よろしくお願いします 785

四、容斥原理与鸽巢原理,《组合数学(第4版)》卢开澄 卢华明

回顾一下加法法则:事件 A 有 m 种产生方式,事件 B 有 n 种产生方式,则事件 A 或 B 之一有 m + n 种产生方式。但是如果 |A| = m,|B| = n,A∩B∅A∩B∅,则|A ∪ B| = m + n。在算竞中,经常遇到复杂的计数问题,多种情况不能直接加法法则相加,如何做到计数不重不漏?很多时候都要用到容斥原理的思想。容斥的计数思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来;然后再把计数时重复计算的数目排斥出去。

EQUINOX1的博客 1341
上一篇: 尼姆博弈 (合集)
下一篇: Polya定理及例题
OneLine_
博客等级 码龄9年 48粉丝 72原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址