Kattis - barcode

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代码:

    

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1e6 + 5;
 5 LL f[maxn];
 6 template<typename T>
 7 T pow_mod(T a,T b,T c)
 8 {
 9     T ans = 1;
10     a %= c;
11     while(b)
12     {
13         if(b&1) ans = (ans*a)%c;
14         b >>= 1;
15         a = (a*a)%c;
16     }
17     return ans;
18 }
19 template<typename T>
20 T inv(T x,T p)
21 {
22     return pow_mod(x,p-2,p);
23 }
24 template<typename T>
25 T C(T n,T m,T p)
26 {
27     if(m > n) return 0;
28     if(m == 0||m == n) return 1;
29     if(m == 1||m == n-1) return n;
30     m = min(m,n-m);
31     T up = 1,down = 1;
32     for(int i=n-m+1;i<=n;i++) up = (up*i)%p;
33     for(int i=1;i<=m;i++) down = (down*i)%p;
34     return (up * inv(down,p))%p;
35 }
36 template<typename T>
37 T Lucas(T n,T m,T p)
38 {
39     if(m == 0) return 1;//边界
40     return (Lucas(n/p,m/p,p) * C(n%p,m%p,p))%p;
41 }
42 //同下
43 //template<typename T>
44 //T Lucas(T n,T m,T p)
45 //{
46 //    T ans = 1;
47 //    while(m)
48 //    {
49 //        ans = (ans * C(n%p,m%p,p))%p;
50 //        n /= p;
51 //        m /= p;
52 //    }
53 //    return ans;
54 //}
55 LL Fibnaci(LL n,LL mod)
56 {
57     f[1] = 2;
58     f[2] = 3;
59     for(int i=3;i<=n;i++)
60         f[i] = (f[i-1] + f[i-2])%mod;
61     return f[n];
62 }
63 
64 int main()
65 {
66     int T;
67     LL n,m;
68     scanf("%d",&T);
69     while(T--)
70     {
71         scanf("%lld%lld",&n,&m);
72         if(n & 1)
73         {
74             cout << Fibnaci(n,m) << endl;
75         }
76         else
77         {
78             cout << (((Fibnaci(n,m) + Lucas(n,n/2,m) - (n/2 + 1))%m)+m)%m << endl;
79         }
80     }
81     return 0;
82 }

 

转载于:https://www.cnblogs.com/chen-tian-yuan/p/11239028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值