Submission #67546393
Source Code Expand
#include<bits/stdc++.h>
#define int long long
using namespace std;
bool isPalindrome(const string& s) {
int l = 0, r = s.size()-1;
while(l < r) {
if(s[l++] != s[r--]) return false;
}
return true;
}
string convertToBase(int n, int b) {
if(n == 0) return "0";
string ans;
while(n > 0) {
ans += to_string(n % b);
n /= b;
}
reverse(ans.begin(), ans.end());
return ans;
}
void generatePalindromes(int N, set<int>& palindromes) {
// Single-digit palindromes
for(int i = 1; i <= 9; ++i) {
palindromes.insert(i);
}
// Generate palindromes with even and odd lengths
for(int length = 2; length <= to_string(N).length(); ++length) {
int half = (length + 1) / 2;
int start = pow(10, half - 1);
int end = pow(10, half) - 1;
for(int num = start; num <= end; ++num) {
string prefix = to_string(num);
string suffix = prefix.substr(0, length / 2);
reverse(suffix.begin(), suffix.end());
string str = prefix + suffix;
int candidate = stoll(str);
if(candidate <= N) {
palindromes.insert(candidate);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int A, N;
cin >> A >> N;
set<int> palindromes;
generatePalindromes(N, palindromes);
int sum = 0;
for(int num : palindromes) {
if(num > N) continue;
string baseA = convertToBase(num, A);
if(isPalindrome(baseA)) {
sum += num;
}
}
cout << sum << "\n";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | C - Palindromic in Both Bases |
| User | Sarthak_Borse |
| Language | C++ 20 (gcc 12.2) |
| Score | 350 |
| Code Size | 1761 Byte |
| Status | AC |
| Exec Time | 1143 ms |
| Memory | 97564 KiB |
Compile Error
Main.cpp: In function ‘void generatePalindromes(long long int, std::set<long long int>&)’:
Main.cpp:31:32: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
31 | for(int length = 2; length <= to_string(N).length(); ++length) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 350 / 350 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt |
| All | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00-sample-01.txt | AC | 1 ms | 3788 KiB |
| 00-sample-02.txt | AC | 800 ms | 97452 KiB |
| 00-sample-03.txt | AC | 856 ms | 97408 KiB |
| 01-01.txt | AC | 1 ms | 3436 KiB |
| 01-02.txt | AC | 1 ms | 3516 KiB |
| 01-03.txt | AC | 1143 ms | 97468 KiB |
| 01-04.txt | AC | 915 ms | 97448 KiB |
| 01-05.txt | AC | 806 ms | 97496 KiB |
| 01-06.txt | AC | 878 ms | 97492 KiB |
| 01-07.txt | AC | 824 ms | 97564 KiB |
| 01-08.txt | AC | 789 ms | 97528 KiB |
| 01-09.txt | AC | 972 ms | 97484 KiB |
| 01-10.txt | AC | 857 ms | 97364 KiB |
| 01-11.txt | AC | 1 ms | 3764 KiB |
| 01-12.txt | AC | 41 ms | 9172 KiB |
| 01-13.txt | AC | 1 ms | 3852 KiB |
| 01-14.txt | AC | 42 ms | 9400 KiB |
| 01-15.txt | AC | 1 ms | 3740 KiB |
| 01-16.txt | AC | 3 ms | 3960 KiB |
| 01-17.txt | AC | 1 ms | 3796 KiB |
| 01-18.txt | AC | 3 ms | 4020 KiB |
| 01-19.txt | AC | 934 ms | 92768 KiB |
| 01-20.txt | AC | 2 ms | 3684 KiB |
| 01-21.txt | AC | 2 ms | 3944 KiB |
| 01-22.txt | AC | 1 ms | 3608 KiB |
| 01-23.txt | AC | 2 ms | 3868 KiB |
| 01-24.txt | AC | 1 ms | 3920 KiB |
| 01-25.txt | AC | 2 ms | 3772 KiB |
| 01-26.txt | AC | 2 ms | 3904 KiB |
| 01-27.txt | AC | 32 ms | 8132 KiB |
| 01-28.txt | AC | 1 ms | 3620 KiB |
| 01-29.txt | AC | 1 ms | 3796 KiB |
| 01-30.txt | AC | 1 ms | 3476 KiB |