zoukankan      html  css  js  c++  java
  • A dreamstart的催促 (快速幂) B TRDD got lost again

      A   dreamstart的催促

    链接:https://ac.nowcoder.com/acm/contest/322/A
    来源:牛客网

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    有一天集训队的学弟们正在计算一堆数,但是dreamstart感觉他们算的太慢了,就让他们坐在一起想出一个快速计算的方法,但是由于他们一时想不出来,想让你帮助他们。他们说现在有一个数列,要算出第 i 个数的 i 次幂并且把每个数计算出来的值加到一起,最后答案模10000019。

    聪明的你可以帮助他们吗?

    输入描述:

    第一行有一个整数n,n <= 1e5

    接下来一行有n个数,每个数的大小不超过1e16

    输出描述:

    输出取模之后的和
    示例1

    输入

    复制
    4
    1 6 9 12

    输出

    复制
    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;
    }
     
    View Code
    //快速幂
    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;
    }
    View Code
     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
    ",sum);
    46       
    47   
    48     return 0;
    49 }
    View Code
     
    X城市是一个交通十分不便利的城市,城市可以看成一个n * m大小的矩阵, 现在TRDD手里有该城市的地图:一个2*n+1行, 2 *m+1列大小的地图。现在TRDD所在的格子用S表示,机场所在的格子用T表示。 其他格子用空格表示,地图上左右相邻的两个格子如果不能通行用"|"表示, 上下相邻的两个点如果不能通行用"-"表示,”+“表示格子的四个角。 题目保证城市X最外圈无法通行(具体请看样例输入)。
    为了能尽快赶到机场,TRDD想请你帮忙计算出他到达机场最少需要走过多少个格子(包括起点S和终点T)。
    如果无法到达机场T,则输出"TRDD Got lost...TAT"(不包括双引号)。
     
    链接:https://ac.nowcoder.com/acm/contest/322/B
    来源:牛客网

    示例1

    输入

    复制
    4 3
    +-+-+-+
    |S| | |
    + +-+-+
    | | | |
    + +-+-+
    | |T  |
    + +-+ +
    |     |
    +-+-+-+

    输出

    复制
    8

    说明

    TRDD所在的位置为(1, 1), 机场的位置为(3, 2)
    路线为(1, 1) -> (2, 1) -> (3, 1) -> (4, 1) -> (4,2) -> (4,3) -> (3,3) ->(3,2)
    共8个格子
    示例2

    输入

    复制
    3 3
    +-+-+-+
    |S|   |
    + + +-+
    | | |T|
    + + +-+
    |   | |
    +-+-+-+

    输出

    复制
    TRDD Got lost...TAT

    说明

    无法从S到达T

    备注:

    由于数据量过大,建议不要使用scanf("%c")读入,否则可能会TLE。
     
    如果输入样例显示格式有误, 请参考图片:
     
     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('
    ');}
     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
    ",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 }
    View Code
  • 相关阅读:
    我要好offer之 二叉树大总结
    我要好offer之 字符串相关大总结
    楼层扔鸡蛋问题[转]
    Linux System Programming 学习笔记(十一) 时间
    Linux System Programming 学习笔记(十) 信号
    Linux System Programming 学习笔记(九) 内存管理
    Linux System Programming 学习笔记(八) 文件和目录管理
    Linux System Programming 学习笔记(七) 线程
    Linux System Programming 学习笔记(六) 进程调度
    APUE 学习笔记(十一) 网络IPC:套接字
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10201671.html
Copyright © 2011-2022 走看看