ブルーブリッジカップ整数スプライシング(思考)

解決策については、https//blog.csdn.net/weixin_44775484/article/details/107442900を参照してください。 

 // #pragma GCC optimize(2)
 #include <iostream>
 #include <cstdio>
 #include <algorithm>
 #include <queue>
 #include <cmath>
 #include <string>
 #include <vector>
 #include <stack>
 #include <map>
 #include <sstream>
 #include <cstring>
 #include <set>
 #include <cctype>
 #include <bitset>
 #define IO                       \
     ios::sync_with_stdio(false); \
     // cout.tie(0);
 using namespace std;
//int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double pi = acos(-1);
LL a[maxn];
LL b[maxn];   // ÿһ¸ö a[i] µÄλÊý 
LL c[12][maxn]; // a[i] µÄ 10 µÄ£¨1~10£©´Î·½ ¶Ô kÈ¡Óà
LL n,k,ans=0;
LL qpow(LL a,LL b)
{
	LL ans=1;
	while(b)
	{
		if(b&1)
			ans=ans*a%k;
		a=a*a%k;
		b=b>>1;
	}
	return ans%k;
}
int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
#endif
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		b[i]=log10(a[i])+1; 
	}	 
	for(int i=0;i<n;i++)
	{
		ans+=c[b[i]][(k-a[i]%k)%k];
		for(int j=1;j<=10;j++)
			c[j][a[i]%k*qpow(10,j)%k]++;
	}
	reverse(a,a+n);
	reverse(b,b+n);
	memset(c,0,sizeof c);
	for(int i=0;i<n;i++)
	{
		ans+=c[b[i]][(k-a[i]%k)%k];
		for(int j=1;j<=10;j++)
			c[j][a[i]%k*qpow(10,j)%k]++;

	}
	cout<<ans;
	return 0;
}

 

おすすめ

転載: blog.csdn.net/qq_44115065/article/details/109056616