cf451E Devu and Flowers 卢卡斯定理+容斥定理

本文介绍了一个组合数学问题的解决方法,利用卢卡斯定理求解从多个盒子中取出特定数量小球的方案数。通过插板法得出基本方案数,并运用容斥原理排除非法情况。

题目:http://codeforces.com/problemset/problem/451/E

题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数。


组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理.

卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p)


从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况。

还要考虑哪些盒子取超了。

违法情况和 = 1个盒子取超的情况数 - 2个的 + 3个的 ....(容斥定理)

然后拿总方案数减去违法的,就是最终结果。总方案数 = C(s+n-1,n-1)


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const ll mod = 1e9+7;
const ll MOD = 1e9+7;

ll k_pow(ll a,ll n,ll p){
    ll ans = 1ll, mo = a;
    while(n){
        if(n&1) ans = (ans * mo)%p;
        mo = (mo * mo)%p;
        n /= 2;
    }
    return ans;
}

ll cal(ll n,ll m,ll p){
    if(m>n) return 0;
    ll ans = 1ll;
    ll wt = 1ll;
    if(m>n-m) m = n-m;
    for(int i=1;i<=m;i++){
        ans = (ans * (n-i+1))%p;
        wt = (wt * i)%p;
    }
    wt = k_pow(wt,p-2,p);
    ans = (ans * wt)%p;
    return ans;
}


ll lucas(ll n,ll m,ll p){
    if(m==0) return 1;
    return ( lucas(n/p,m/p,p) * cal(n%p,m%p,MOD) )%p;
}


int n;
ll s,f[30];
int main(){
    while(scanf("%d%I64d",&n,&s)!=EOF){
        ll sum = 0;
        for(int i=0;i<n;i++){
            scanf("%I64d",&f[i]);
            sum += f[i];
        }
        ll ans = 0;
        for(int st=0;st<(1<<n);st++){
            int fl = 1;
            ll ts = s;
            for(int i=0;i<n;i++) if((1<<i)&st){
                fl *= -1;
                ts -= f[i]+1;
            }
            if(ts<0) continue;
            ans = (ans + fl*lucas(ts+n-1,n-1,MOD))%MOD;
        }
        ans = (ans + MOD)%MOD;
        cout<<ans<<endl;
    }
    return 0;
}


<think>我们正在处理的是为IMX6ULL移植桌面版Ubuntu系统。根据引用内容,我们注意到:引用[1]提到了tslib触摸库的路径设置,这提示我们在移植过程中可能需要处理触摸屏驱动。引用[2]提到了uboot移植的环境变量设置,以及交叉编译工具链的路径设置。由于IMX6ULL是ARMCortex-A7架构,而Ubuntu官方并没有为imx6ull提供官方的桌面版镜像,因此我们需要通过构建rootfs并配置桌面环境来实现。步骤概述:1.准备交叉编译环境和开发环境。2.获取并编译uboot(针对imx6ull)。3.获取并配置Linux内核(针对imx6ull)。4.构建根文件系统(使用UbuntuBase),并安装桌面环境(如LXDE、XFCE等轻量级桌面)。5.配置系统服务、驱动(包括触摸屏等)和网络。6.生成系统镜像并烧录。详细步骤:###1.环境准备安装必要的工具,设置交叉编译工具链。```bashsudoaptupdatesudoaptinstallgitmakegccg++bisonflexlibssl-devlibncurses-devu-boot-toolsdevice-tree-compilergcc-arm-linux-gnueabihf```下载并设置交叉编译工具链(如使用arm-linux-gnueabihf):```bash#假设我们将工具链安装在/opt目录下sudotarxvfgcc-arm-linux-gnueabihf-*.tar.xz-C/optexportPATH=/opt/gcc-arm-linux-gnueabihf/bin:$PATHexportARCH=armexportCROSS_COMPILE=arm-linux-gnueabihf-```###2.获取并编译U-Boot从官方或开发板供应商处获取U-Boot源码,并针对imx6ull进行配置和编译。```bashgitclonehttps://github.com/u-boot/u-boot.gitcdu-boot#切换到稳定版本,例如v2021.04gitcheckoutv2021.04-bimx6ull#配置为imx6ull的默认配置(具体配置根据开发板而定,例如mx6ull_14x14_evk)makemx6ull_14x14_evk_defconfigmake```编译完成后,得到u-boot.imx文件。###3.获取并编译Linux内核从官方或开发板供应商处获取Linux内核源码。```bashgitclonehttps://github.com/nxp-imx/linux-imx.gitcdlinux-imx#切换到合适的分支,例如imx_4.19.35_1.1.0gitcheckoutimx_4.19.35_1.1.0#配置内核makeimx_v7_defconfig#使用imx6ull的默认配置#如果需要,可以进一步配置内核(比如添加触摸屏驱动等)makemenuconfig#编译内核和设备树makezImagedtbs-j4```###4.构建根文件系统使用UbuntuBase构建根文件系统,并安装桌面环境。下载UbuntuBase20.04(注意选择armhf架构):```bashwgethttp://cdimage.ubuntu.com/ubuntu-base/releases/20.04/release/ubuntu-base-20.04.5-base-armhf.tar.gzmkdirubuntu_rootfssudotar-xpfubuntu-base-20.04.5-base-armhf.tar.gz-Cubuntu_rootfs```配置根文件系统,安装桌面环境(这里以LXDE为例):```bash#挂载必要的文件系统sudomount-tproc/procubuntu_rootfs/procsudomount-tsysfs/sysubuntu_rootfs/syssudomount-obind/devubuntu_rootfs/devsudomount-obind/dev/ptsubuntu_rootfs/dev/pts#进入chroot环境sudochrootubuntu_rootfs#在chroot环境中aptupdateaptinstalllanguage-pack-en-basesudosshnetwork-managerlxde-corelxdmxorg#安装LXDE桌面#设置root密码passwdroot#创建普通用户并设置密码adduserubuntu#设置主机名echo"imx6ull-ubuntu">/etc/hostname#退出chrootexit#卸载sudoumountubuntu_rootfs/procsudoumountubuntu_rootfs/syssudoumountubuntu_rootfs/dev/ptssudoumountubuntu_rootfs/dev```###5.配置系统####5.1配置触摸屏(如果需要)参考引用[1],如果需要触摸屏,可以移植tslib并配置。将tslib编译后,将库和配置文件放入根文件系统,并设置环境变量。####5.2配置显示确保内核中已经启用了正确的显示驱动(如mxsfb),并配置Xorg使用fbdev驱动。####5.3配置网络确保内核中网络驱动(如FEC)已启用,并在根文件系统中配置NetworkManager或netplan。###6.制作系统镜像创建一个空的镜像文件,分区并格式化,然后将根文件系统复制进去。```bash#创建镜像文件(假设4GB)ddif=/dev/zeroof=ubuntu-desktop.imgbs=1Mcount=4096sudopartedubuntu-desktop.img--script--mklabelmsdossudopartedubuntu-desktop.img--script--mkpartprimaryext41MiB4095MiB#挂载镜像并格式化sudolosetup-fPubuntu-desktop.imgsudomkfs.ext4/dev/loop0p1sudomount/dev/loop0p1/mnt#复制根文件系统sudocp-aubuntu_rootfs/*/mntsudoumount/mntsudolosetup-d/dev/loop0```###7.烧录镜像将U-Boot、内核、设备树和根文件系统镜像烧录到SD卡或eMMC中。具体步骤取决于开发板的启动方式。通常步骤:1.使用dd命令将U-Boot写入SD卡的开头:```bashsudoddif=u-boot.imxof=/dev/sdXbs=1kseek=1conv=fsync```2.将SD卡分为两个分区:第一个分区(FAT32)存放内核和设备树,第二个分区(ext4)作为根文件系统。3.复制内核(zImage)和设备树(*.dtb)到第一个分区。4.将根文件系统镜像解压或复制到第二个分区。###8.启动开发板连接串口,上电启动,进入系统后登录并配置桌面环境。注意事项:-由于IMX6ULL资源有限,建议使用轻量级桌面(如LXDE、XFCE)。-确保内核配置中包含了必要的驱动(显示、触摸、网络等)。-根据开发板的具体硬件调整设备树。§§相关问题§§1.如何为IMX6ULL的Ubuntu系统优化启动时间?2.IMX6ULL运行Ubuntu桌面版时如何降低内存占用?3.如何在IMX6ULL上配置双屏显示?4.如何为IMX6ULL的Ubuntu系统配置硬件加速?5.如何解决IMX6ULL运行Ubuntu桌面时的触摸屏校准问题?</think>### 在IMX6ULL上移植桌面版Ubuntu系统的详细步骤 #### 1. **准备交叉编译环境** ```bash # 安装基础工具 sudo apt update sudo apt install git make gcc g++ bison flex libssl-dev libncurses-dev u-boot-tools # 设置交叉编译工具链(以arm-buildroot-linux-gnueabihf为例) export ARCH=arm export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- export PATH=$PATH:/your/toolchain/path/bin # 替换为实际路径[^2] ``` #### 2. **获取U-Boot源码并配置** ```bash git clone https://github.com/u-boot/u-boot.git cd u-boot make mx6ull_14x14_evk_defconfig # 根据开发板型号选择 make menuconfig # 配置DRAM参数、串口等 make ``` 生成文件:`u-boot.imx` #### 3. **编译Linux内核** ```bash git clone https://github.com/nxp-imx/linux-imx.git cd linux-imx git checkout imx_4.19.35_1.1.0 # 选择稳定分支 make imx_v7_defconfig # 配置桌面支持 make menuconfig # 启用关键选项: # Device Drivers → Graphics support → FB_MXS # Device Drivers → Input device support → Touchscreens # File systems → Enable POSIX ACLs # General setup → Desktop-oriented features make zImage dtbs -j$(nproc) ``` 生成文件:`arch/arm/boot/zImage` 和 `.dtb`设备树文件 #### 4. **构建根文件系统** ```bash # 下载Ubuntu Base wget http://cdimage.ubuntu.com/ubuntu-base/releases/20.04/release/ubuntu-base-20.04.5-base-armhf.tar.gz mkdir ubuntu_rootfs sudo tar -xpf ubuntu-base-20.04.5-base-armhf.tar.gz -C ubuntu_rootfs ``` #### 5. **安装桌面环境** ```bash # 挂载虚拟文件系统 sudo mount -t proc /proc ubuntu_rootfs/proc sudo mount -t sysfs /sys ubuntu_rootfs/sys sudo mount -o bind /dev ubuntu_rootfs/dev sudo mount -o bind /dev/pts ubuntu_rootfs/dev/pts # 进入chroot环境 sudo chroot ubuntu_rootfs # 在chroot中执行 apt update apt install --no-install-recommends ubuntu-desktop lxdm xorg network-manager passwd root # 设置root密码 adduser ubuntu # 创建普通用户 exit # 卸载 sudo umount ubuntu_rootfs/{proc,sys,dev/pts,dev} ``` #### 6. **配置触摸屏支持** ```bash # 编译tslib(参考引用[1]) cd ~ git clone https://github.com/libts/tslib.git cd tslib ./autogen.sh ./configure --host=arm-buildroot-linux-gnueabihf --prefix=/opt/tslib make sudo make install # 复制到根文件系统 sudo cp -r /opt/tslib ubuntu_rootfs/usr/local/ ``` #### 7. **创建系统镜像** ```bash # 创建4GB镜像 dd if=/dev/zero of=ubuntu-desktop.img bs=1M count=4096 sudo parted ubuntu-desktop.img mklabel msdos sudo parted ubuntu-desktop.img mkpart primary ext4 1MiB 100% # 格式化并挂载 sudo losetup -fP ubuntu-desktop.img sudo mkfs.ext4 /dev/loop0p1 sudo mount /dev/loop0p1 /mnt # 复制文件系统 sudo cp -a ubuntu_rootfs/* /mnt sudo umount /mnt ``` #### 8. **烧录到开发板** 1. 准备TF卡: ```bash sudo dd if=u-boot.imx of=/dev/sdX bs=1k seek=1 conv=fsync ``` 2. 分区: - 第1分区(FAT32):存放zImage和.dtb - 第2分区(ext4):存放根文件系统 3. 复制文件: ```bash sudo cp zImage /media/boot/ sudo cp *.dtb /media/boot/ sudo tar -xpf ubuntu-desktop.img -C /media/rootfs/ ``` #### 9. **首次启动配置** 1. 连接串口控制台 2. 登录后执行: ```bash systemctl enable lxdm # 启用桌面登录管理器 systemctl start lxdm ``` 3. 校准触摸屏(如需要): ```bash ts_calibrate ``` #### 10. **优化桌面体验** ```bash # 在开发板上执行 sudo apt install --no-install-recommends lxde-core # 轻量级桌面 sudo apt remove gnome-software ubuntu-software # 移除冗余组件 # 配置GPU加速(IMX6ULL支持OpenGL ES) sudo apt install imx-gpu-viv-tools ``` > **关键注意事项**: > 1. IMX6ULL内存有限(通常512MB-1GB),建议使用LXDE/LXQT轻量桌面 > 2. 确保内核配置中启用FB_MXS显示驱动 > 3. 触摸屏需在设备树中正确配置`&i2c1`节点 > 4. 首次启动时间较长(需生成SSH密钥等)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值