Kattis - barcode

题目原文:

To prepare for ACM-ICPC 2017 in Saigon, the host univeristy – Ho Chi Minh city University of Education (HCMUE) – decided to print barcodes on the participants’ t-shirts. The barcode requirement needs to be simple to reduce the cost but still show some scientific style. HCMUE decided that every barcode consists of red bars and blue bars satisfing at least one of the following conditions:

l The number of blue bars is equal to the number of red bars.

l There are no 22 consecutive blue bars.

Let K denote the number of different ways to create the required barcodes containing bars. Given two integers and M, where M is a prime number, your task is to help them identify the remainder of divided by M.

Input

The input consists of several datasets. The first line of the input contains the number of datasets, which is a positive number and is not greater than 20. Each dataset is described by one line containing two numbers N and M (1≤N≤106,1<M≤107). M is a prime number.

Output

For each dataset, write in one line the remainder of K divided by M.

题目大意:

为一个长度为n的条形码制定一个涂色方案(红色和蓝色两种),要求满足下列条件之一:

  1. 红色和蓝色数量相等
  2. 蓝色条形码不连续

输出方案数(对素数M取余)

题目解析:

  1. 对于给出的样例很容易发现这是个Fibnaci数列的一部分,但是如果写出当N=4时的情况,发现结果为11,不是Fibnaci预期中的8。如果你继续验证会发现,当N为奇数时,结果是个Fibnaci数,当N为偶数,总比Fibnaci数大。
  2. 偶数与奇数的不同是偶数可以整除2,也就是偶数可以满足第一种情况,而奇数不可以;打表或者自己写出当N=4,6,8的情况,也可以验证多出的数字就是当红色等于蓝色时,条形码连续的情况。(注意当颜色相同时,也会有蓝色条形码不连续的情况,根据结果。这时默认是符合第二种情况的)
  3. 只需计算这个数即可。

    得:数量相等且连续 = 数量相等 - 数量相等且不连续;

    利用组合数: ans   = C(n,n/2) - C(n/2+1,n/2)

              = C(n,n/2) - C(n/2+1,1)

              = C(n,n/2) - n/2 - 1

    当N为奇数 res = F[n]%M(不是完整的Fibnaci,要从F[1] = 2,F[2] = 3开始计算)

    当N为偶数 res = (F[n] + ans)%M

    程序涉及了Lucas定理和逆元

   参考博客:https://www.cnblogs.com/fzl194/p/9095177.html

         https://www.cnblogs.com/scx2015noip-as-php/p/lucas.html

AC代码:

    

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + ;
LL f[maxn];
template<typename T>
T pow_mod(T a,T b,T c)
{
T ans = ;
a %= c;
while(b)
{
if(b&) ans = (ans*a)%c;
b >>= ;
a = (a*a)%c;
}
return ans;
}
template<typename T>
T inv(T x,T p)
{
return pow_mod(x,p-,p);
}
template<typename T>
T C(T n,T m,T p)
{
if(m > n) return ;
if(m == ||m == n) return ;
if(m == ||m == n-) return n;
m = min(m,n-m);
T up = ,down = ;
for(int i=n-m+;i<=n;i++) up = (up*i)%p;
for(int i=;i<=m;i++) down = (down*i)%p;
return (up * inv(down,p))%p;
}
template<typename T>
T Lucas(T n,T m,T p)
{
if(m == ) return ;//边界
return (Lucas(n/p,m/p,p) * C(n%p,m%p,p))%p;
}
//同下
//template<typename T>
//T Lucas(T n,T m,T p)
//{
// T ans = 1;
// while(m)
// {
// ans = (ans * C(n%p,m%p,p))%p;
// n /= p;
// m /= p;
// }
// return ans;
//}
LL Fibnaci(LL n,LL mod)
{
f[] = ;
f[] = ;
for(int i=;i<=n;i++)
f[i] = (f[i-] + f[i-])%mod;
return f[n];
} int main()
{
int T;
LL n,m;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
if(n & )
{
cout << Fibnaci(n,m) << endl;
}
else
{
cout << (((Fibnaci(n,m) + Lucas(n,n/,m) - (n/ + ))%m)+m)%m << endl;
}
}
return ;
}

Kattis - barcode的更多相关文章

  1. php barcode 制作二条码,隐藏条码的内容,只保留条码

    <?php global $_W, $_GPC; $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display'; require_once ...

  2. 用Barcode生成条形码图片

    使用第三方类库:BarcodeLib.dll private BitmapImage GenerateBarcodeBitmap(string visitId) { BarcodeLib.Barcod ...

  3. 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理

    在“如何用MediaCapture解决二维码扫描问题”这篇文章中,我们通过“成像”.“截图”与“识别”三个步骤介绍了使用MediaCapture扫码的主要过程及注意事项.本文主要针对“识别”的过程,对 ...

  4. .NET 中Barcode Library的应用二

    .NET中Barcode Library的应用二 介绍 在上一篇中我已经简单介绍了这个函数库(条形码应用之一------------函数库的简介).在这一篇中我将使用这个库提供更多的操作,希望对大家有 ...

  5. 使用Spire.Barcode程序库生成二维码

    使用Spire.Barcode程序库生成二维码 某天浏览网页发现了一个二维码的程序库.它的描述说他可以扫描二维码图像.我很感兴趣,想试试他是不是会有用.所以我就用了些方法扫描二维码图像来测试一下.结果 ...

  6. JQUERY PLUGIN:BARCODE条形码插件

    1)query.barcode.js安装 同其他jquery插件一样,只需要将jquery框架和jquery.barcode.js导入页面即可. <script type="text/ ...

  7. atitit.条形码的原理与生成总结java Barcode4j barcode o5

    atitit.条形码的原理与生成总结java Barcode4j barcode o5 条形码类库使用报告Barcode4j, ZXing 1 使用成果图片 1 条形码标准code 128和code  ...

  8. 支持单色条码图像生成的条形码控件Barcode Professional

    Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...

  9. 创建条形码图像易用的控制字符编码功能的条形码控件Native Crystal Reports Barcode Generator

    Native Crystal Reports Barcode Generator是一个对象,它可以很容易地被嵌入到一个Crystal Report中用于创建条形码图像.一旦此条形码被安装在一个报表中, ...

随机推荐

  1. JS 提取公式中的参数

    'A+B-C/D*E'.split(/[*/()+-]/)  => [A,B,C,D,E]

  2. Nginx代理与反向代理、负载均衡实践

    通过 Nginx 提供的反向代理和负载均衡功能,可以合理的完成业务的分配,提高网站的处理能力;同时利用缓存功能,还可以将不需要实时更新的动态页面输出结果,转化为静态网页形成缓存,从而提高网站的响应速度 ...

  3. 怎样理解Functor与Monad

    1. 复合函数操作符 Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c Prelude> (.) ( ...

  4. 购物车1.0版——python第5天

    # 输出商品列表,用户输入序号,显示选中商品名称# 商品li = ['手机', '电脑', '耳机', '键盘', '鼠标']# 要求:1.页面显示序号+商品名称如下# 1 手机# 2 电脑# 3 耳 ...

  5. 好1.1.4 PTA提交列表及说明

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 我在这个课程的目标是 这个作业在那个具体方面帮助我实现目标 概括本周的学习以及更加熟练本周的代码 参考文献 C语言程序设计 百度文献 (h ...

  6. static_cast关键字 dynamic_cast关键字

    前言 说起C++中的继承.多态.虚函数等概念,可能很多同学都有所了解,但是要说真正熟知的同学可能就不是很多了.最近在编程过程中了解到C++类型的层次转换(这就涉及到了多态和继承的相关概率),通常C语言 ...

  7. Visual Assist 10.9.2248 破解版(支持VS2017) 转载

    自己在Windows10下同时安装了VS2017和VS2013,先装的VS2017和Visual Assist,后装的VS2013,发现VS2013中没显示Visual Assist,Google了一 ...

  8. Python开发简介

    年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承 . 最新的TIOBE排行榜,Python已经占据世界第四名的位置, Python崇尚优美 ...

  9. UVA - 11624 J - Fire! (BFS)

    题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...

  10. MVC使用Area:CS0234: 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)

    一,如图: 解决方法是:将区域生成的的文件夹下的web.config中的using System.Web.Optimization删掉 如下,Area文件目录找到Web.config Web.conf ...