problemscpp
A collection of my answers to algorithm problems in c++.
函数
pat::b::b1104 命名空间参考

1104 天长地久 更多...

函数

void dfs (string str, const int current_i, const int m, const int k, const int current_sum, const int cnt9, vector< string > &ans)
 
bool is_prime (int n)
 
int main (istream &cin, ostream &cout)
 
 TEST (b1104, case1)
 

详细描述

1104 天长地久

函数说明

◆ dfs()

void pat::b::b1104::dfs ( string  str,
const int  current_i,
const int  m,
const int  k,
const int  current_sum,
const int  cnt9,
vector< string > &  ans 
)

在文件 pat.cpp3721 行定义.

3721 {
3722 if(current_i + cnt9 == k) {
3723 if(const int r = m - current_sum - cnt9 * 9; (current_i > 1 && r >= 0 || current_i == 1 && r > 0) && r < 9) {
3724 str += static_cast<char>(r + '0');
3725 } else {
3726 return;
3727 }
3728 for(int i = 0; i < cnt9; i++) {
3729 str += '9';
3730 }
3731 ans.emplace_back(str);
3732 return;
3733 }
3734 for(int i = current_i == 1 ? 1 : 0; i <= 9; i++) {
3735 if(!(current_sum + i + cnt9 * 9 > m || current_sum + i + (k - current_i) * 9 - 1 < m)) {
3736 dfs(str + static_cast<char>(i + '0'), current_i + 1, m, k, current_sum + i, cnt9, ans);
3737 }
3738 }
3739 }
int dfs(const vector< unordered_set< int > > &g, int father, int nd)
Definition: pat.cpp:5022

引用了 dfs().

被这些函数引用 dfs() , 以及 main().

◆ is_prime()

bool pat::b::b1104::is_prime ( int  n)

在文件 pat.cpp3709 行定义.

3709 {
3710 if(n == 1) {
3711 return false;
3712 }
3713 for(int i = 2; i <= sqrt(n); i++) {
3714 if(n % i == 0) {
3715 return false;
3716 }
3717 }
3718 return true;
3719 }

被这些函数引用 main().

◆ main()

int pat::b::b1104::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp3675 行定义.

3675 {
3676 int N;
3677 cin >> N;
3678 vector<int> diff(10);
3679 for(int i = 0; i < 10; i++) {
3680 diff[i] = 1 - i * 9;
3681 }
3682 for(int i = 1; i <= N; i++) {
3683 bool ok = false;
3684 int k;
3685 int m;
3686 cin >> k >> m;
3687 cout << "Case " << i << endl;
3688 for(int j = k; j >= 1; j--) {
3689 const int n = m + diff[j];
3690 const int g = gcd(m, n);
3691 if(n > 0 && g > 2 && is_prime(gcd(m, n))) {
3692 vector<string> ans;
3693 dfs("", 1, m, k, 0, j, ans);
3694 if(!ans.empty()) {
3695 ok = true;
3696 for(const auto &str: ans) {
3697 cout << m + diff[j] << ' ' << str << endl;
3698 }
3699 }
3700 }
3701 }
3702 if(!ok) {
3703 cout << "No Solution" << endl;
3704 }
3705 }
3706 return 0;
3707 }
const int N
Definition: acwing.h:146
bool is_prime(unsigned int n)
Definition: pat.cpp:4422

引用了 dfs(), is_prime() , 以及 acwing::acwing2019::N.

被这些函数引用 TEST().

◆ TEST()

pat::b::b1104::TEST ( b1104  ,
case1   
)

在文件 pat_test.cpp1832 行定义.

1832 {
1833 istringstream in("2\n"
1834 "6 45\n"
1835 "7 80");
1836 auto out = ostringstream();
1837 main(in, out);
1838 const auto ans = out.str();
1839 ASSERT_EQ("Case 1\n"
1840 "10 189999\n"
1841 "10 279999\n"
1842 "10 369999\n"
1843 "10 459999\n"
1844 "10 549999\n"
1845 "10 639999\n"
1846 "10 729999\n"
1847 "10 819999\n"
1848 "10 909999\n"
1849 "Case 2\n"
1850 "No Solution\n",
1851 out.str());
1852 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().