برای حل این مسئله، نیازمند یک رویکرد داینامیک پروگرمینگ هستیم که بتواند تعداد ترتیبهای مختلف قطعات را که باعث میشوند رقم کل گاوصندوق بر ۱۱ بخشپذیر باشد، محاسبه کند. با توجه به محدودیتهای زمانی و حافظهای، باید روشی کارآمد و بهینه پیادهسازی کنیم.
تحلیل مسئله
تقسیمبندی قطعات
هر قطعه دارای دو ویژگی مهم است:
- طول (len): تعداد ارقام در قطعه.
- باقیمانده وقتی بر ۱۱ مدوله میشود (mod 11): باقیمانده عدد قطعه هنگام تقسیم بر ۱۱.
برای سادهسازی، قطعات را بر اساس این دو ویژگی طبقهبندی میکنیم. از آنجا که طول میتواند بر مکانیسم تغییر پرادی تأثیر بگذارد، ما فقط به باقیمانده طول به ازای حالت زوج یا فرد توجه میکنیم. به این ترتیب تعداد حالتهای ممکن برای هر قطعه ۲ (زوج یا فرد) × ۱۱ (باقیماندههای ممکن) = ۲۲ حالت است.
حالتهای DP
برای پیادهسازی داینامیک پروگرمینگ، حالتهای زیر را تعریف میکنیم:
- پرادی قبلی (parity): مجموع طول قطعاتی که تا به حال انتخاب شدهاند به ازای حالتی زوج یا فرد است.
- باقیمانده فعلی (mod): باقیمانده مجموع مقادیر قطعات انتخاب شده به ازای تقسیم بر ۱۱.
بنابراین، حالت DP به شکل dp[parity][mod] خواهد بود.
انتقال حالات
برای هر نوع قطعه (با ویژگیهای (p, m) که p پرادی و m باقیمانده است):
- تغییر پرادی:
- اگر
p = 0 (زوج)، پرادی تغییر نخواهد کرد.
- اگر
p = 1 (فرد)، پرادی تغییر میکند (از زوج به فرد یا برعکس).
- تغییر باقیمانده:
- با توجه به اینکه در مبنای ۱۰،
10^k mod 11 = (-1)^k، اگر پرادی قبل از اضافه کردن قطعه زوج باشد، مقدار قطعه به صورت مثبت، و در غیر این صورت به صورت منفی به مجموع اضافه خواهد شد.
این انتقال به شکل زیر مدلسازی میشود:
new_mod = (current_mod + (sign * m)) mod 11
new_parity = (current_parity + p) mod 2
که در آن sign = 1 اگر current_parity = 0 و sign = -1 اگر current_parity = 1.
پیادهسازی
در کد زیر، ابتدا قطعات را بر اساس ویژگیهای (p, m) طبقهبندی میکنیم و سپس از الگوریتم داینامیک پروگرمینگ برای محاسبه تعداد ترتیبهای معتبر استفاده میکنیم.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
// Group the pieces by (parity, mod)
// There are 2 parities and 11 mods
// cnt[p][m] = number of pieces with parity p and mod m
vector<vector<int>> cnt(2, vector<int>(11, 0));
for(int i=0;i<n;i++){
string s;
cin >> s;
int len = s.size();
int p = len %2;
// Compute a_i mod 11
int a_mod =0;
for(char c:s){
a_mod = (a_mod *10 + (c-'0')) %11;
}
cnt[p][a_mod]++;
}
// Initialize DP
// dp[p][m] = number of ways with current parity p and sum m
// Because we are dealing with permutations, we need to account for factorial
// We will use DP where we add one piece at a time
// However, n=2000 is too large, so we need to optimize
// We can iterate through all pieces grouped by (p,m)
// and for each group, apply combinatorial transitions
// Initialize DP table
vector<vector<int>> dp(2, vector<int>(11, 0));
dp[0][0] =1;
// Total factorial
// Precompute factorial and inverse factorial
// Not necessary here as we're counting permutations step by step
for(int p_type=0; p_type<=1; p_type++){
for(int m_type=0; m_type<=10; m_type++){
if(cnt[p_type][m_type]==0) continue;
int c = cnt[p_type][m_type];
// We need to add c pieces of type (p_type, m_type)
// For each piece, we need to consider inserting it into all possible positions
// But with DP, we need to iterate c times
for(int _=0; _<c; _++){
// Temporary DP for this step
vector<vector<int>> tmp(2, vector<int>(11,0));
for(int p=0; p<=1; p++){
for(int m=0; m<11; m++){
if(dp[p][m]==0) continue;
// Place current piece after current sum
// sign = (-1)^p
// If p=0, sign=1; if p=1, sign=-1
int sign = (p==0)?1:-1;
int new_m = (m + (sign * m_type)) %11;
if(new_m <0) new_m +=11;
int new_p = (p + p_type) %2;
tmp[new_p][new_m] = (tmp[new_p][new_m] + (int)((long long)dp[p][m] * (c -_)) % MOD ) % MOD;
}
}
// Update DP
for(int p=0; p<=1; p++) {
for(int m=0; m<11; m++) {
dp[p][m] = tmp[p][m];
}
}
}
}
}
// The answer is dp[?][0], where ? can be 0 or 1 depending on final permutation
// But since we need the total sum to be 0 mod 11, regardless of final parity
// However, in our transitions, sum was handled correctly
// So the answer is dp[p][0] for both p=0 and p=1
ll answer = (dp[0][0] + dp[1][0]) % MOD;
cout << answer;
}
توضیح کد
-
طبقهبندی قطعات:
- قطعات بر اساس
parity (زوج یا فرد بودن طول) و mod 11 طبقهبندی میشوند و تعداد هر نوع در جدول cnt ذخیره میشود.
-
ابتداییسازی DP:
- جدول
dp با ابعاد [2][11] برای نگهداری تعداد ترتیبهایی که با پرادی و باقیمانده مشخصی ختم میشوند، تنظیم میشود. ابتدا فقط حالت (0, 0) با مقدار ۱ وجود دارد.
-
انتقال حالات:
- برای هر نوع قطعه
(p_type, m_type)، و به تعداد c بار که این نوع قطعه در لیست وجود دارد:
- به ازای هر قطعه از این نوع، جدول
dp بهروزرسانی میشود.
- برای هر حالت فعلی
(p, m)، بسته به parity فعلی، با استفاده از sign، مقادیر جدید محاسبه و تعداد ترتیبها بهروزرسانی میشوند.
-
محاسبه پاسخ نهایی:
- مجموع تعداد ترتیبهایی که باقیمانده نهایی آنها برابر با ۰ است، به عنوان پاسخ نهایی استخراج میشود.
پیچیدگی زمانی و حافظه
-
زمان اجرا: به ازای هر نوع قطعه و هر بار اضافه کردن قطعه، وضعیتهای dp بهروزرسانی میشوند. با طبقهبندی به ۲۲ نوع و حداکثر ۲۰۰۰ قطعه، پیچیدگی زمانی تقریباً قابل قبول است برای محدودیتهای مسئله.
-
حافظه: استفاده از جدول dp با ابعاد [2][11] مصرف حافظه بسیار کمی دارد و مشکلی ایجاد نمیکند.
پاسخ
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
// Group the pieces by (parity, mod)
// There are 2 parities and 11 mods
// cnt[p][m] = number of pieces with parity p and mod m
vector<vector<int>> cnt(2, vector<int>(11, 0));
for(int i=0;i<n;i++){
string s;
cin >> s;
int len = s.size();
int p = len %2;
// Compute a_i mod 11
int a_mod =0;
for(char c:s){
a_mod = (a_mod *10 + (c-'0')) %11;
}
cnt[p][a_mod]++;
}
// Initialize DP
// dp[p][m] = number of ways with current parity p and sum m
// Because we are dealing with permutations, we need to account for factorial
// We will use DP where we add one piece at a time
// However, n=2000 is too large, so we need to optimize
// We can iterate through all pieces grouped by (p,m)
// and for each group, apply combinatorial transitions
// Initialize DP table
vector<vector<int>> dp(2, vector<int>(11, 0));
dp[0][0] =1;
// Total factorial
// Precompute factorial and inverse factorial
// Not necessary here as we're counting permutations step by step
for(int p_type=0; p_type<=1; p_type++){
for(int m_type=0; m_type<=10; m_type++){
if(cnt[p_type][m_type]==0) continue;
int c = cnt[p_type][m_type];
// We need to add c pieces of type (p_type, m_type)
// For each piece, we need to consider inserting it into all possible positions
// But with DP, we need to iterate c times
for(int _=0; _<c; _++){
// Temporary DP for this step
vector<vector<int>> tmp(2, vector<int>(11,0));
for(int p=0; p<=1; p++){
for(int m=0; m<11; m++){
if(dp[p][m]==0) continue;
// Place current piece after current sum
// sign = (-1)^p
// If p=0, sign=1; if p=1, sign=-1
int sign = (p==0)?1:-1;
int new_m = (m + (sign * m_type)) %11;
if(new_m <0) new_m +=11;
int new_p = (p + p_type) %2;
tmp[new_p][new_m] = (tmp[new_p][new_m] + (int)((long long)dp[p][m] * (c -_)) % MOD ) % MOD;
}
}
// Update DP
for(int p=0; p<=1; p++) {
for(int m=0; m<11; m++) {
dp[p][m] = tmp[p][m];
}
}
}
}
}
// The answer is dp[?][0], where ? can be 0 or 1 depending on final permutation
// But since we need the total sum to be 0 mod 11, regardless of final parity
// However, in our transitions, sum was handled correctly
// So the answer is dp[p][0] for both p=0 and p=1
ll answer = (dp[0][0] + dp[1][0]) % MOD;
cout << answer;
}