HDU 5895 Mathematician QSC

题目地址

欧拉函数+矩阵快速幂

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<string.h>
  4 #include<queue>
  5 #define LL long long
  6 using namespace std;
  7 const int Nmax=10;
  8 LL n,y,x,s,tmp;
  9 int mod;
 10 int oula_mod;
 11
 12 struct Matrix
 13 {
 14     int n,m;
 15     long long map[Nmax][Nmax];
 16     Matrix(int x,int y)
 17     {
 18         n=x;m=y;
 19         for(int i=1;i<=n;i++)
 20             for(int j=1;j<=m;j++)
 21                 map[i][j]=0;
 22     }
 23     Matrix operator * (const Matrix b)
 24     {
 25         Matrix c(n,b.m);
 26         if(m==b.n)
 27         {
 28             for(int i=1;i<=c.n;i++)
 29                 for(int j=1;j<=c.m;j++)
 30                     for(int k=1;k<=m;k++)
 31                         c.map[i][j]=(c.map[i][j]+(map[i][k]*b.map[k][j])%oula_mod)%oula_mod;
 32             return c;
 33         }
 34         printf("error!!!!!!!!!!!!!!\n");
 35     }
 36 };
 37
 38
 39 int oula(int n)
 40 {
 41     int ret=1,i;
 42     for(i=2;i*i<=n;i++)
 43     {
 44         if(n%i==0)
 45         {
 46             n/=i,ret*=i-1;
 47             while(n%i==0) n/=i,ret*=i;
 48         }
 49     }
 50     if(n>1) ret*=n-1;
 51     return ret;
 52 }
 53
 54
 55
 56
 57 Matrix get(long long n)
 58 {
 59     Matrix base(4,4);
 60     base.map[1][1]=1;base.map[1][2]=1;base.map[1][3]=0;base.map[1][4]=0;
 61     base.map[2][1]=0;base.map[2][2]=4;base.map[2][3]=1;base.map[2][4]=4;
 62     base.map[3][1]=0;base.map[3][2]=1;base.map[3][3]=0;base.map[3][4]=0;
 63     base.map[4][1]=0;base.map[4][2]=2;base.map[4][3]=0;base.map[4][4]=1;
 64     Matrix ans(4,4);
 65     for(int i=1;i<=ans.n;i++)
 66         ans.map[i][i]=1;
 67
 68     while(n>0)
 69     {
 70         if(n & 1)
 71             ans=ans*base;
 72         base=base*base;
 73         n>>=1;
 74     }
 75
 76     return ans;
 77 }
 78
 79 long long get_ans(long long times)
 80 {
 81     long long ans=1;
 82     long long base=x;
 83     while(times>0)
 84     {
 85         if(times & 1)
 86             ans=(ans*base)%mod;
 87         base=(base*base)%mod;
 88         times>>=1;
 89     }
 90     return ans;
 91 }
 92
 93
 94 int main()
 95 {
 96
 97     int t;
 98     scanf("%d",&t);
 99     while(t--)
100     {
101         scanf("%lld%lld%lld%lld",&n,&y,&x,&s);
102         mod=s+1;
103         oula_mod=oula(mod);
104         //printf("oula_mod:%d\n",oula_mod);
105         Matrix base(4,1);
106         base.map[1][1]=0;
107         base.map[2][1]=1;
108         base.map[3][1]=0;
109         base.map[4][1]=0;
110         Matrix ans=get(n*y)*base;
111         long long mi=ans.map[1][1]+oula_mod;
112         //printf("mi:%lld\n",mi );
113         //continue;
114         printf("%lld\n",get_ans(mi));
115     }
116     return 0;
117 }
时间: 2024-10-13 18:40:43

HDU 5895 Mathematician QSC的相关文章

HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/details/52577212 [分析]一开始想简单了,对于a^x mod p这种形式的直接用欧拉定理的数论定理降幂了 结果可想而知,肯定错,因为题目并没有保证gcd(x,s+1)=1,而欧拉定理的数论定理是明确规定的 所以得另谋出路 那么网上提供了一种指数循环节降幂的方法 具体证明可以自行从网上找一找 有

hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law. Through unremitting e

HDU 5895 矩阵快速幂+高次幂取模

HDU 5895 Mathematician QSC 题意:已知f(n)=2*f(n-1)+f(n-2), g(n)=∑f(i)²(0<=i<=n), 给出n,x,y,s, 求x^(g(n*y))%(s+1); 思路:OEIS查到了g(n)=f(n)*f(n+1)/2, f(n)可以用矩阵快速幂求得, 有一个定理可以用于高次幂取模 x^n %k=x^(n%phi(k)+phi(k)) %k, 此处phi(x)为欧拉函数,但是在对幂次取模时存在一个除2, 又因为(a/b)%k=(a%bk)/b,

hdu-5895 Mathematician QSC(数学)

题目链接: Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 189    Accepted Submission(s): 90 Problem Description QSC dream of becoming a mathematician, he believes that everything

hdu 5895(矩阵快速幂+欧拉函数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2*f(n-1)*f(n-1); 2*f(n-1)*f(n-1)=f(n)*f(n-1)-f(n-2)*f(n-1); 累加可得 g(n) = f(n)*f(n+1)/2 然后这个公式:A^x % m = A^(x%phi(m)+phi(m)) % m (x >= phi(m)) 反正比赛没做出来.

2016.9.18 --- Shenyang ol

1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j’s Biological Test 1004 Mathematician QSC 1005 Running King 1006 The Game 1007 odd-even number 1008 oasis in desert 1009 QSC and Master 1010 Count primes 哈哈哈哈...在高铁上打代码哟><

HDU 5900 QSC and Master (区间DP)

题目链接   http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列Ai.key和Ai.value,若当前相邻的两个数Ai.key和Ai+1.key的最大公约数大于1,则可以把这两个数消去,同时消去Ai.value和Ai+1.value,每次消去得到的分数为Ai和Ai+1的value值,问最大可能得分. 注意:当Ai和Ai+1被消去后,Ai-1和Ai+2成为了新的相邻的数.若符合条件则可以继续消去. 思路:很明显是区间DP,但是我比赛中如何也

hdu 5900 QSC and Master 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Every school has some legends, Northeastern University is the same. Enter from the north gate of Northeastern University,You are

HDU 5900 - QSC and Master [ DP ]

题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2  移除34以后12也相邻了,可以移除 分析: 先预处理出所有GCD[l][r], 意味 l <= i <= r的区域可以全部移除, 用记忆化搜索处理 然后 dp[i] 代表到 i 为止可以得到的最大value和 if (G[l][r]) dp[r] = max(dp[r], dp[l-1] + sum[r