链接:https://ac.nowcoder.com/acm/contest/322/A
来源:牛客网
有一天集训队的学弟们正在计算一堆数,但是dreamstart感觉他们算的太慢了,就让他们坐在一起想出一个快速计算的方法,但是由于他们一时想不出来,想让你帮助他们。他们说现在有一个数列,要算出第 i 个数的 i 次幂并且把每个数计算出来的值加到一起,最后答案模10000019。
聪明的你可以帮助他们吗?
第一行有一个整数n,n <= 1e5
接下来一行有n个数,每个数的大小不超过1e16
输出取模之后的和
21502
快速幂求解就行
/** /*快速幂板子 /* */ ll mod_pow(ll x , ll n ,ll mod){ if(n==0) return 1; ll res = mod_pow(x * x % mod, n / 2 , mod); if(n % 1) res = res * x % mod; return res; }
//快速幂 typedef long long ll; ll mod_pow(ll x ,ll n , ll mod){ ll res; while(n > 0){ if(n & 1) res = res * x % mod; x = x * x % mod; n >>=1; } return res; }
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<ctime> 7 #include<iostream> 8 #include<algorithm> 9 #include<stack> 10 #include<queue> 11 #include<vector> 12 #include<list> 13 #include<map> 14 #include<set> 15 using namespace std; 16 typedef long long ll; 17 long long a[100000+10]; 18 const long long mod=10000019; 19 int poww(int a, int b) { 20 ll ans = 1, base = a; 21 while (b != 0) { 22 if (b & 1 != 0) 23 ans = ans*base%mod; 24 // base = base*base%mod; 25 b >>= 1; 26 } 27 return ans; 28 } 29 30 31 32 int main() { 33 //long long x; 34 int n; 35 36 scanf("%d",&n); 37 long long sum = 0; 38 for(int i=1;i<=n;i++) 39 { 40 scanf("%lld",&a[i]); 41 //a[i] %= mod; 42 sum=(sum +poww(a[i],i))%mod; 43 } 44 //int num = sum; 45 printf("%lld\n",sum); 46 47 48 return 0; 49 }
1 #pragma GCC optimize(3,"Ofast","inline") 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 bool Finish_read; 6 template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch==‘-‘)f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar();x*=f;Finish_read=1;} 7 template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+‘0‘);} 8 template<class T>inline void writeln(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);putchar(‘\n‘);} 9 template<class T>inline void write(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);} 10 /*================Header Template==============*/ 11 const int step[4][2]={{0,1},{1,0},{-1,0},{0,-1}}; 12 char mp[6005][6005]; 13 string now; 14 int n,m,sx,sy,ex,ey,dis[6005][6005]; 15 typedef pair<int,int>pii; 16 #define fi first 17 #define se second 18 int main() { 19 ios::sync_with_stdio(false); 20 cin.tie(0),cout.tie(0); 21 cin>>n>>m; 22 getline(cin,now); 23 for(int i=1;i<=2*n+1;++i) { 24 getline(cin,now); 25 for(int j=1;j<=2*m+1;++j) { 26 mp[i][j]=now[j-1],dis[i][j]=1e9; 27 if(mp[i][j]==‘S‘) 28 sx=i,sy=j; 29 if(mp[i][j]==‘T‘) 30 ex=i,ey=j; 31 } 32 } 33 queue<pii>q; 34 q.push(pii(sx,sy)),dis[sx][sy]=0; 35 for(pii u;!q.empty();q.pop()) { 36 u=q.front(); 37 int x=u.fi,y=u.se; 38 if(x==ex&&y==ey) 39 return 0*printf("%d\n",dis[x][y]/2+1); 40 for(int k=0;k<4;++k) { 41 int nx=x+step[k][0],ny=y+step[k][1]; 42 if((mp[nx][ny]==‘ ‘||mp[nx][ny]==‘T‘)&&dis[nx][ny]>dis[x][y]+1) 43 dis[nx][ny]=dis[x][y]+1,q.push(pii(nx,ny)); 44 } 45 } 46 puts("TRDD Got lost...TAT"); 47 }
A dreamstart的催促 B TRDD got lost again
原文:https://www.cnblogs.com/DWVictor/p/10201671.html