HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)

思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定.

快速幂入门第一题QAQ

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <ctype.h>
//******************************************************
#define lrt (rt*2)
#define rrt  (rt*2+1)
#define LL long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define exp 1e-8
//***************************************************
#define eps             1e-8
#define inf             0x3f3f3f3f
#define INF             2e18
#define LL              long long
#define ULL             unsigned long long
#define PI              acos(-1.0)
#define pb              push_back
#define mk              make_pair

#define all(a)          a.begin(),a.end()
#define rall(a)         a.rbegin(),a.rend()
#define SQR(a)          ((a)*(a))
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define Iterator(a)     __typeof__(a.begin())
#define rIterator(a)    __typeof__(a.rbegin())
//#define FastRead      ios_base::sync_with_stdio(0);cin.tie(0)
#define FastRead        ios::sync_with_stdio(false);
#define CasePrint       pc(‘C‘); pc(‘a‘); pc(‘s‘); pc(‘e‘); pc(‘ ‘); write(qq++,false); pc(‘:‘); pc(‘ ‘)
#define vi              vector <int>
#define vL              vector <LL>
#define For(I,A,B)      for(int I = (A); I < (B); ++I)
#define FOR(I,A,B)      for(int I = (A); I <= (B); ++I)
#define rFor(I,A,B)     for(int I = (A); I >= (B); --I)
#define Rep(I,N)        For(I,0,N)
#define REP(I,N)        FOR(I,1,N)
using namespace std;
const int maxn=1e5+10;
#define mod 998244353

struct Matirx {
    int h,w;
    LL a[5][5];
}ori,res,it;
LL f[5]={0,1,1};
void iint(){
    it.w=2;it.h=1;it.a[1][1]=1;it.a[1][2]=0;
    res.w=res.h=2;
    memset(res.a,0,sizeof(res.a));
    res.a[1][1]=res.a[2][2]=1;
    ori.w=ori.h=2;
    memset(ori.a,0,sizeof(ori.a));
    ori.a[1][1]= 1;ori.a[1][2]= 1;
    ori.a[2][1]= 1;ori.a[2][2]= 0;
}
Matirx multy(Matirx x,Matirx y){
    Matirx z;z.w=y.w;z.h=x.h;
    memset(z.a,0,sizeof(z.a));
    for(int i=1;i<=x.h;i++){
        for(int k=1;k<=x.w;k++){
            if(x.a[i][k]==0) continue;
            for(int j=1;j<=y.w;j++)
                z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
        }
    }
    return z;
}

LL Matirx_mod(LL n){
    if(n<2) return f[n];
    else n-=1;
    while(n){
        if(n&1) res=multy(ori,res);
        ori=multy(ori,ori);
        n>>=1;
    }
    res=multy(it,res);
    return res.a[1][1]%mod;
}

int main()
{
    LL n;
    while(scanf("%I64d",&n)!=EOF)
    {
        iint();
        printf("%I64d\n",Matirx_mod(2*n+3)-1);
    }
    return 0;
}
时间: 2024-12-22 05:13:58

HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)的相关文章

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】

Problem Description We define a sequence F: ? F0=0,F1=1;? Fn=Fn?1+Fn?2 (n≥2). Give you an integer k, if a positive number n can be expressed byn=Fa1+Fa2+...+Fak where 0≤a1≤a2≤?≤ak, this positive number is mjf?good. Otherwise, this positive number is 

HDU 6205(尺取法)2017 ACM/ICPC Asia Regional Shenyang Online

题目链接 emmmm...思路是群里群巨聊天讲这题是用尺取法.....emmm然后就没难度了,不过时间上3000多,有点.....盗了个低配本的读入挂发现就降到2800左右, 翻了下,发现神犇Claris280MS秒过.......%%% #include <stdio.h> #include <stdlib.h> #include <cmath> #include <string.h> #include <iostream> #include

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=6215 Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destr

2017 ACM/ICPC Asia Regional Shenyang Online

cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2084    Accepted Submission(s): 1348 Problem Description Connecting the display screen and signal sources which produce differen

hdu6195 cable cable cable(from 2017 ACM/ICPC Asia Regional Shenyang Online)

最开始一直想不通,为什么推出这个公式,后来想了半天,终于想明白了. 题目大意是,有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个格子中的哪K个,都可以与K个物品恰好一一匹配. 然后你可以试着画图,每次必须有k个格子是单独的(与各物体只有一条线相连)所以还剩下m-k个格子,可以用来补位,也就是跟每个物品都相连,所以就有(m-k)*k 上代码(巨巨巨巨巨简单): 1 #include <cstdio> 2 #include <cstring>

2017 ACM/ICPC Asia Regional Shenyang Online 记录

这场比赛全程心态爆炸…… 开场脑子秀逗签到题WA了一发.之后0贡献. 前期状态全无 H题想复杂了,写了好久样例过不去. 然后这题还是队友过的…… 后期心态炸裂,A题后缀数组理解不深,无法特判k = 1时的情况. 然后也没有心思读题了,心静不下来. 比赛题目链接 Problem B $ans = k(n - k + 1)$ #include <bits/stdc++.h> using namespace std; typedef long long LL; LL n, k; int main()

2017 ACM/ICPC Asia Regional Shenyang Online array array array

2017-09-15 21:05:41 writer:pprp 给出一个序列问能否去掉k的数之后使得整个序列不是递增也不是递减的 先求出LIS,然后倒序求出最长递减子序列长度,然后判断去k的数后长度是否都大于所求长度 代码如下: #include <bits/stdc++.h> using namespace std; int arr1[100005],tmp1[100005],arr2[100005], tmp2[100005]; int len1,len2; int main() { in

hdu6201 transaction transaction transaction(from 2017 ACM/ICPC Asia Regional Shenyang Online)

最开始一直想着最短路,不过看完题解后,才知道可以做成最长路.唉,还是太菜了. 先上图: 只要自己添加两个点,然后如此图般求最长路即可,emmm,用SPFA可以,迪杰斯特拉也可以,或者别的都ok,只要通过一次即可. 上代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 #include <algorithm> 6 using na