H. The Nth Item(The 2019 Asia Nanchang First Round Online Programming Contest)

题意:https://nanti.jisuanke.com/t/41355

给出N1,计算公式:A=F(N)Ni=Ni-1 ^ (A*A),F为类斐波那契需要矩阵快速幂的递推式。

求第k个N。

思路:

发现从大约1e5个数开始N交替出现,到一定位置%2即可。(or正解:https://blog.csdn.net/qq_41848675/article/details/100667808   or

https://blog.csdn.net/jk_chen_acmer/article/details/100635672   or   map记忆化)

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 //const int maxn=1000005;
  4 using namespace std;
  5 typedef  long  long  ll;
  6 const int N = 2;//矩阵大小
  7 //ll k;
  8 const long long mod=(long long )998244353;
  9 struct Mat
 10 {
 11     ll mat[N][N];
 12     Mat operator*(const Mat a)const
 13     {
 14         Mat b; memset(b.mat, 0, sizeof(b.mat));
 15         for (int i = 0; i < N; i++)
 16             for (int j = 0; j < N; j++)
 17                 for (int k = 0; k < N; k++)
 18                     b.mat[i][j] = (b.mat[i][j] + (mat[i][k]) *(a.mat[k][j])) % mod;
 19         return b;
 20     }
 21 };
 22
 23 ll phi(ll x)//求欧拉
 24 {
 25     ll res=x;
 26     for(ll i=2;i*i<=x;i++)
 27     {
 28         if(x%i==0)
 29         {
 30             res=res/i*(i-1);
 31             while(x%i==0) x/=i;
 32         }
 33     }
 34     if(x>1) res=res/x*(x-1);
 35     return res;
 36 }
 37 Mat Pow(Mat m, ll k)
 38 {
 39     //if(k==1) return 1;
 40     Mat ans;
 41     memset(ans.mat, 0, sizeof(ans.mat));
 42     for (int i = 0; i < N; i++)
 43         ans.mat[i][i] = 1;
 44     while (k)
 45     {
 46         if (k & 1) ans = ans*m;
 47         k >>= 1;
 48         m = m*m;
 49     }
 50     return ans;
 51 }
 52
 53 ll que[4*2000000];
 54 int head=0,tail=1 ;
 55 int main() {
 56
 57     ll phi_mod=phi(mod);
 58     Mat m;
 59     int q;
 60     ll n,ans;
 61     scanf("%d%lld",&q,&n);
 62     //printf("\n%d %lld\n",q,n);
 63     Mat f;
 64     m.mat[0][0]=3;m.mat[0][1]=2;
 65     m.mat[1][0]=1;m.mat[1][1]=0;
 66     f.mat[0][0]=1;//x1
 67     f.mat[1][0]=0;//x0
 68     f = Pow(m, (n-1)%phi_mod)*f;
 69     ans=f.mat[0][0];
 70     //printf("%lld %lld\n",n,ans);
 71     que[head]=ans;
 72
 73
 74
 75
 76     //printf("%lld",f.mat[0][0]);
 77 /*
 78  * 10000000 1000000000000000000
 79  * */
 80     for (register int i = 2; i <= q; i++) {
 81         //init(m);
 82         //memset(f.mat, 0, sizeof(f.mat));
 83         n = n ^ (f.mat[0][0] * f.mat[0][0]);
 84
 85         if (n == 0) {
 86             f.mat[0][0] = 0;
 87         } else if (n == 1) {
 88             f.mat[0][0] = 1;
 89         } else {
 90             m.mat[0][0] = 3;
 91             m.mat[0][1] = 2;
 92             m.mat[1][0] = 1;
 93             m.mat[1][1] = 0;
 94             f.mat[0][0] = 1;//x1
 95             f.mat[1][0] = 0;//x0
 96
 97             f = Pow(m, (n - 1) % phi_mod) * f;
 98
 99         }
100         ans = ans ^ f.mat[0][0];
101         //printf("%lld %lld\n", n, ans);
102
103
104         que[tail++]=ans;
105         if(tail-head>4)
106         {
107             head++;
108             if(que[head]==que[head+2]&&que[head+1]==que[head+1+2])
109             {
110                 int tmp=q-i;
111                 if(tmp%2)
112                 {
113                     ans=que[head];
114                 }
115                 else
116                 {
117                     ans=que[head+1];
118                 }
119                 break;
120             }
121         }
122
123
124     }
125     printf("%lld\n",ans);
126     return 0;
127 }
128
129 /*
130  * 858251072
131  *
132  * 245284867829898842 447003402
133     485245887812443738 1008229130
134  *
135  *
136  *
137  *
138  * */

原文地址:https://www.cnblogs.com/--HPY-7m/p/11493509.html

时间: 2024-08-30 16:40:55

H. The Nth Item(The 2019 Asia Nanchang First Round Online Programming Contest)的相关文章

Fire-Fighting Hero (The 2019 Asia Nanchang First Round Online Programming Contest)

This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VV(Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE

The 2019 Asia Nanchang First Round Online Programming Contest The Nth Item

The Nth Item 思路: 先用特征根法求出通向公式,然后通向公式中出现了\(\sqrt{17}\),这个可以用二次剩余求出来,然后可以O(\(log(n)\))求出. 但是还不够,我们先对\(n\)欧拉降幂,然后求base为\(\sqrt{1e9}\)的快速幂,预处理一些东西,就可以类似O(1)求出了. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/std

Pangu Separates Heaven and Earth(签到题)(The 2019 Asia Nanchang First Round Online Programming Contest)

Long long ago, the sky and the earth were not separated, and the universe was chaotic. There was a giant named Pangu who slept for eighteen thousand years in this chaos. One day, Pangu woke up suddenly. When he saw the darkness around him, he took up

The 2019 Asia Nanchang First Round Online Programming Contest E. Magic Master

题目链接:https://nanti.jisuanke.com/t/41352 题目意思还是好理解的,看过的人不多,感觉是被通过量吓到了.其实就是个水题,反向模拟就好了, 用队列模拟,反向模拟,它要放m张卡到后面,那我就放m张卡到前面,一开始队列只有1张卡,慢慢加到n张卡, 先加大的卡,再一直到1的卡.对了,可能会出现只有5张卡,m却是6,7,8或9,10,那么为了减少不必要的模拟, 用mod来减少,因为有些模拟会让卡和之前比较,算是原封不动. 1 #include <iostream> 2

The 2019 Aisa Nanchang First Round Online Programming Contest ——H

思路:矩阵快速幂+map 代码: #include<cmath> #include<algorithm> #include<cstdio> #include<map> const int INF=0x3f3f3f3f; const int maxn = 1e7+10; const int mod=998244353; using namespace std; typedef long long ll; map<ll,ll> mp; ll n; s

The 2019 Asia Yinchuan First Round Online Programming F. Moving On

t题目链接:https://nanti.jisuanke.com/t/41290 思路:题目意思很容易想到floyd,但是由于危险度的限制,我们该怎么跑floyd呢. 一开始理解错题目了,以为u->v包括终点起点都不能超过给的危险度,不过看样例,好像只需要中间的城市不能超过危险度. 我们可以这么想,每个城市都有一个危险度,而floyd算法的本质是i到j经过前k个城市的转移,得到多源最短路,那么我们不妨 记录城市的编号和危险度,然后按城市的危险度排序,重新编号,危险度小的先跑floyd,然后f[k

.c和.h文件的区别(头文件与之实现文件的的关系~ )

 .c和.h文件的区别 一个简单的问题:.c和.h文件的区别 学了几个月的C语言,反而觉得越来越不懂了.同样是子程序,可以定义在.c文件中,也可以定义在.h文件中,那这两个文件到底在用法上有什么区别呢? 2楼: 子程序不要定义在.h中. 函数定义要放在.c中,而.h只做声明.否则多引用几次,就会发生函数重复定义的错误. 3楼: .h只做声明,编译后不产生代码   4楼: 这样做目的是为了实现软件的模块化 使软件结构清晰,而且也便于别人使用你写的程序 纯粹用 C 语言语法的角度,你当然可以在 .h

2019 ICPC Universidad Nacional de Colombia Programming Contest

https://codeforc.es/gym/102307 最后5题收尾了,大概率铜.其中有2题是签到手速题.有一题是抄模板的表达式求值,写一个分数类随便过. 比较有趣的是下面的: G. Graduation 题意:一共有n门课,每门课有至多1门先修课,没有先修课的用0表示.学习一门课的条件是:这个学期选的课不足k门,且这门课的先修课(假如有的话)已经在前面的学期中学过了.求最少需要多少个学期学完. 思路:队友一开始是优先拓展儿子数最多的子结点,我觉得怪怪的,比如一个像蒲公英一样的东西,一开始

线程编程指南(Threading Programming Guide)

简介 线程是一种技术,可以在一个应用中同时执行多个代码路径.尽管新技术如操作对象和GCD提供一个更现代和更高效的工具来实现并发,OS X 和iOS也提供接口来创建和管理线程. 本文揭示了OS X中可用的线程包并展示了如何使用它们.本文还描述了应用程序中支持线程和多线程代码同步的相关技术. 重要:如果你正在开发一个新的应用,鼓励你研究实现并发的OS X技术.尤其是你不熟悉实现线程应用所需要的设计技术.这些可选择的技术简化你实现执行并发路径的工作量并提供相对传统线程更好的性能.关于这些技术的信息,参