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. mysql for Mac 下创建数据表中文显示为?的解决方法

    在我的绝版Mac mini下安装了mysql 5.7版本,实例中,在通过load data 导入数据时发现表中的中文显示为  ? 通过百度,发现多个版本的解决方法,将其中一个成功解决的方法贴上来: 大 ...

  2. C++中函数调用操作符的重载

    1,本博文讲述函数对象问题: 2,客户需求: 1,编写一个函数: 1,函数可以获得斐波那契数列每项的值: 2,每调用一次返回一个值: 3,函数可根据需要重复使用: 4,代码示例: ; i<; i ...

  3. Debug your ASP.NET Application while Hosted on IIS

    转摘:http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS This ...

  4. JavaScript — event介绍以及兼容处理

    JavaScript - event介绍以及兼容处理 1.事件流 浏览器发展到第四代时(IE4及 Netscape Communicator 4),浏览器开发团队遇到一个问题:页面的哪个部分会拥有某个 ...

  5. js转换成字符串

    有两种方法: 1.对于boolean, number, string类型,可调用toString()方法 2.用String(var)方法 其中,第二种方法使用范围更广,可将没有toString()方 ...

  6. springboot中xml配置之@ImportResource

    springboot中进行相关的配置往往有java配置和xml配置两种方式. 使用java的方式配置只需要使用@configuration注解即可,而使用xml的方式配置的话需要使用@ImportRe ...

  7. Android中Parcelable的原理和使用方法

    Parcelable的简单介绍 介绍Parcelable不得不先提一下Serializable接口,Serializable是Java为我们提供的一个标准化的序列化接口,那什么是序列化呢? 进行And ...

  8. 大哥带我走渗透ii--时间盲注,布尔盲注

    5/27 基于布尔的盲注 我连题目都看不懂555,先去补充一点知识.https://blog.csdn.net/weixin_40709439/article/details/81355856 返回的 ...

  9. 转载:对比Angular/jQueryUI/Extjs:没有一个框架是万能的

    Angular不能做什么?对比Angular/jQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你们两个字 ...

  10. Spring Bean 的加载过程

    Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...