标签:c代码 else res logs 注意 -- text turn ++
此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。
题目链接:https://ly.men.ci/problem/42
分析:
快速幂然后暴力算即可。注意补零操作,另外这题的数据好像有问题,每个字符都要换行输出。
直接快速幂居然就过了...
我还想计算那么多次会不会有冗余,开了个数组记录x^i想直接调用,结果是我炸了...
AC代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 6 const int MAXN = 100003; 7 inline void read(long long &x) 8 { 9 char ch = getchar(),c = ch;x = 0; 10 while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar(); 11 while(ch <= ‘9‘ && ch >= ‘0‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar(); 12 if(c == ‘-‘) x = -x; 13 } 14 15 long long n,k,x,tmp,ans,cnt = 1,mod = 1; 16 char s[20]; 17 18 struct NUM 19 { 20 long long a,b; 21 }num[MAXN]; 22 23 long long ksm(long long a,long long b) 24 { 25 long long base = a,r = 1; 26 for(;b;b>>=1) 27 { 28 if(b&1) 29 r = r*base%mod; 30 base = base*base%mod; 31 } 32 return r; 33 } 34 35 int main() 36 { 37 // freopen("1.in","r",stdin); 38 read(n),read(k); 39 for(register int i = 1;i <= n;++ i) 40 read(num[i].a),read(num[i].b); 41 for(register int i = 1;i <= k;++ i) 42 mod *= 10; 43 read(x); 44 for(register int i = 1;i <= n;++ i) 45 { 46 tmp = ksm(x,num[i].b); 47 ans = (ans+tmp*num[i].a%mod)%mod; 48 } 49 tmp = 0; 50 if(ans == 0) s[++tmp] = ‘0‘; 51 else 52 while(ans > 0) 53 { 54 s[++tmp] = (ans%10)+‘0‘; 55 ans /= 10; 56 } 57 for(int i = 0;i < k-tmp;++ i) 58 printf("0\n"); 59 for(int i = tmp;i >= 1;-- i) 60 printf("%c\n",s[i]); 61 return 0; 62 }
标签:c代码 else res logs 注意 -- text turn ++
原文地址:http://www.cnblogs.com/shingen/p/7662726.html