题意
https://arc020.contest.atcoder.jp/tasks/arc020_3?lang=en
求
一
个
非
常
大
的
数
对
B
取
模
的
值
.
这
个
大
数
用
l
i
个
a
i
连
续
串
接
表
示
.
求一个非常大的数对B取模的值.这个大数用l_i个a_i连续串接表示.
求一个非常大的数对B取模的值.这个大数用li个ai连续串接表示.
题解
真是神思路.
首先我们思考一下.
对于每一个
a
i
a_i
ai来说,我们都可以发现,
比如说
a
i
=
12
a_i=12
ai=12,则
12121212......
=
12
×
1
0
0
+
12
×
1
0
1
+
.
.
.
+
12
×
1
0
n
−
1
12121212......=12\times 10^0+12\times 10^1+...+12\times 10^{n-1}
12121212......=12×100+12×101+...+12×10n−1.
这样子跑一个等比数列求和并求一波逆元即可.
然而这个算法并不能获得满分.
满分算法,把
l
i
l_i
li拆分成二进制的形式,把每一个
a
i
a_i
ai的长度求出,用快速幂的思想爆搞.
每次把长度加倍,对每一个部分取模即可.
谢谢大家.
#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
int x=0,f=1;char c=gc();
for (;!isdigit(c);c=gc()) f^=c=='-';
for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
return f?x:-x;
}
template <typename mitsuha>
inline bool read(mitsuha &x){
x=0;int f=1;char c=gc();
for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
if (!~c) return 0;
for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
return x=f?x:-x,1;
}
template <typename mitsuha>
inline int write(mitsuha x){
if (!x) return 0&pc(48);
if (x<0) pc('-'),x=-x;
int bit[20],i,p=0;
for (;x;x/=10) bit[++p]=x%10;
for (i=p;i;--i) pc(bit[i]+48);
return 0;
}
inline char fuhao(){
char c=gc();
for (;isspace(c);c=gc());
return c;
}
}using namespace chtholly;
using namespace std;
const int yuzu=1e5;
typedef int fuko[yuzu|10];
fuko a,l;
ll kasumi(ll a,ll b,int mod){
ll s=1;
for (;b;b>>=1,a=a*a%mod) if (b&1) s=s*a%mod;
return s;
}
int main() {
int i,j,n=read();
ll ans=0;
for (i=1;i<=n;++i)
read(a[i]),read(l[i]);
int mod=read();
for (i=1;i<=n;++i) {
ll len=0,p=a[i];
for (;a[i];a[i]/=10) ++len;
for (j=0;j<31;++j,len<<=1) {
if (l[i]>>j&1)
ans=(ans*kasumi(10,len,mod)+p)%mod;
p=(p*kasumi(10,len,mod)+p)%mod;
}
}
write(ans),pl;
}