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. 用 Flask 来写个轻博客 (19) — 以 Bcrypt 密文存储账户信息与实现用户登陆表单

    目录 目录 前文列表 修改 User Model Flask Bcrypt 将 Bcrypt 应用到 User Model 中 创建登陆表单 前文列表 用 Flask 来写个轻博客 (1) - 创建项 ...

  2. 3.tensorflow——NN

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data numClasses=10 inp ...

  3. vue2.0---最近总结

    1.用脚手架给运维部门搞了个小东西,重新温习了一遍牛逼又方便的vue-cli. webpack的东西不必关心太多,而且webpack最近也是出了最新的4.0版本.还是改变挺多的,对一些代码的格式要求更 ...

  4. python zip 压缩

    zipfile zip文件操作 引入模块: import zipfile zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下: zipfi ...

  5. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    https://blog.csdn.net/ztf312/article/details/50708302 报错原因: Numpy对逻辑表达式判别不清楚,它可以返回False如果等号两边两个式子是数值 ...

  6. 什么是php递归

    程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一 ...

  7. for语句和if语句画正方形菱形

    public static void main(String[] args) { int n =8; // 空心正方形 for(int i=0;i<=n;i++){ if(i==0||i==n) ...

  8. 一步一步学Vue(九) 路由元数据

    一步一步学Vue(九):https://www.cnblogs.com/Johnzhang/p/7260888.html

  9. C++中的多重继承(二)

    1,本文分析另一个多重继承问题及其工程中的解决方案,单继承加多接口实现的开发方式: 2,多重继承的问题三: 1,多重继承可能产生多个虚函数表: 1,实际工程中可能造成不可思议的问题,并且这些问题很难以 ...

  10. 49.求1+2+3+.......+n

    题目描述:   求1+2+3+...+n的值,要求不能使用乘除法,还有,if,while,for等关键字. 思路分析:   使用递归的解法,但是递归的终止条件需要使用if关键字不符合要求,所以我们利用 ...