output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { ios::sync_with_stdio(0); int n; cin >> n; string str[n + 1]; string an; char ch; for (int i = 0; i < an.size(); i++) { if (an[i] == '?') { an[i] = '#'; } } for (int i = 0; i < n; i++) { cin >> str[i]; } an = str[0]; int p; set<char> ss; for (int i = 0; i < str[0].size(); i++) { p = 0; ss.clear(); for (int j = 0; j < n; j++) { if (str[j][i] == '?') p++; else { ss.insert(str[j][i]); ch = str[j][i]; } } if (p == n) cout << 'z'; else if (ss.size() == 1) cout << ch; else cout << '?'; } return 0; }
### Prompt Create a solution in CPP for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std; int main() { ios::sync_with_stdio(0); int n; cin >> n; string str[n + 1]; string an; char ch; for (int i = 0; i < an.size(); i++) { if (an[i] == '?') { an[i] = '#'; } } for (int i = 0; i < n; i++) { cin >> str[i]; } an = str[0]; int p; set<char> ss; for (int i = 0; i < str[0].size(); i++) { p = 0; ss.clear(); for (int j = 0; j < n; j++) { if (str[j][i] == '?') p++; else { ss.insert(str[j][i]); ch = str[j][i]; } } if (p == n) cout << 'z'; else if (ss.size() == 1) cout << ch; else cout << '?'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9); const long long LINF = (long long)(5e18); template <class T> T nextInt() { T x = 0, p = 1; char c; do { c = getchar(); } while (c <= 32); if (c == '-') { p = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * p; } int met[26]; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; int m = s[0].length(); string ans; for (int i = 0; i < m; i++) { memset(met, 0, sizeof(met)); for (int j = 0; j < n; j++) if (s[j][i] != '?') met[s[j][i] - 'a']++; int k = 0; for (int z = 0; z < 26; z++) if (met[z]) k++; if (k > 1) ans.push_back('?'); else if (k == 1) ans.push_back((max_element(met, met + 26) - met) + 'a'); else ans.push_back('a'); } cout << ans; }
### Prompt Your task is to create a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = int(1e9); const long long LINF = (long long)(5e18); template <class T> T nextInt() { T x = 0, p = 1; char c; do { c = getchar(); } while (c <= 32); if (c == '-') { p = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * p; } int met[26]; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; int m = s[0].length(); string ans; for (int i = 0; i < m; i++) { memset(met, 0, sizeof(met)); for (int j = 0; j < n; j++) if (s[j][i] != '?') met[s[j][i] - 'a']++; int k = 0; for (int z = 0; z < 26; z++) if (met[z]) k++; if (k > 1) ans.push_back('?'); else if (k == 1) ans.push_back((max_element(met, met + 26) - met) + 'a'); else ans.push_back('a'); } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; vector<string> a; bool b[26]; vector<char> c; int main() { ios_base::sync_with_stdio(0); int t; cin >> t; a.resize(t); for (int i = 0; i < t; i++) { cin >> a[i]; } int m = a[0].size(); c.resize(m); for (int i = 0; i < m; i++) { memset(b, 0, sizeof(b)); for (int j = 0; j < t; j++) { if (a[j][i] != '?') b[a[j][i] - 'a'] = 1; } int t1 = 0, t2 = -1; for (int j = 0; j < 26; j++) { if (b[j]) { t2 = j; t1++; } } if (t1 == 0) c[i] = 'a'; else if (t1 == 1) c[i] = 'a' + t2; else c[i] = '?'; } for (int j = 0; j < m; j++) cout << c[j]; cout << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<string> a; bool b[26]; vector<char> c; int main() { ios_base::sync_with_stdio(0); int t; cin >> t; a.resize(t); for (int i = 0; i < t; i++) { cin >> a[i]; } int m = a[0].size(); c.resize(m); for (int i = 0; i < m; i++) { memset(b, 0, sizeof(b)); for (int j = 0; j < t; j++) { if (a[j][i] != '?') b[a[j][i] - 'a'] = 1; } int t1 = 0, t2 = -1; for (int j = 0; j < 26; j++) { if (b[j]) { t2 = j; t1++; } } if (t1 == 0) c[i] = 'a'; else if (t1 == 1) c[i] = 'a' + t2; else c[i] = '?'; } for (int j = 0; j < m; j++) cout << c[j]; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n; for (int i = 0; i < n; i++) { string x; cin >> x; if (i == 0) { for (int j = 0; j < x.size(); j++) s += '-'; } for (int j = 0; j < x.size(); j++) { if (x[j] == '?') continue; if (s[j] == '-') s[j] = x[j]; if (s[j] != x[j]) s[j] = '?'; } } for (int i = 0; i < s.size(); i++) if (s[i] == '-') s[i] = 'a'; cout << s << endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n; for (int i = 0; i < n; i++) { string x; cin >> x; if (i == 0) { for (int j = 0; j < x.size(); j++) s += '-'; } for (int j = 0; j < x.size(); j++) { if (x[j] == '?') continue; if (s[j] == '-') s[j] = x[j]; if (s[j] != x[j]) s[j] = '?'; } } for (int i = 0; i < s.size(); i++) if (s[i] == '-') s[i] = 'a'; cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 5; string s[MAXN]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> s[i]; string ans = ""; for (int j = 0; j < (int)s[0].length(); j++) { bool b = false, b2 = false; char ch = '?'; for (int i = 0; i < n; i++) { if (s[i][j] != '?') { if (ch == '?') { b2 = true; ch = s[i][j]; } else if (s[i][j] != ch) { b = true; } } } if (b) ans += '?'; else if (b2) ans += ch; else ans += 'x'; } cout << ans << endl; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 5; string s[MAXN]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> s[i]; string ans = ""; for (int j = 0; j < (int)s[0].length(); j++) { bool b = false, b2 = false; char ch = '?'; for (int i = 0; i < n; i++) { if (s[i][j] != '?') { if (ch == '?') { b2 = true; ch = s[i][j]; } else if (s[i][j] != ch) { b = true; } } } if (b) ans += '?'; else if (b2) ans += ch; else ans += 'x'; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> const int imax = INT32_MAX; const int imin = INT32_MIN; using namespace std; const long long int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int t = 1; while (t--) { int n; cin >> n; string s[n]; for (int i = 0; i < n; ++i) { cin >> s[i]; } string ans = ""; for (int i = 0; i < s[0].size(); ++i) { unordered_map<char, int> umap; for (int j = 0; j < n; ++j) { if (s[j][i] != '?') umap[s[j][i]]++; } if (umap.size() > 1) { ans += '?'; } else if (umap.size() < 1) { ans += 'a'; } else { auto it = umap.begin(); ans += (it->first); } } cout << ans; } return 0; }
### Prompt Create a solution in cpp for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const int imax = INT32_MAX; const int imin = INT32_MIN; using namespace std; const long long int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int t = 1; while (t--) { int n; cin >> n; string s[n]; for (int i = 0; i < n; ++i) { cin >> s[i]; } string ans = ""; for (int i = 0; i < s[0].size(); ++i) { unordered_map<char, int> umap; for (int j = 0; j < n; ++j) { if (s[j][i] != '?') umap[s[j][i]]++; } if (umap.size() > 1) { ans += '?'; } else if (umap.size() < 1) { ans += 'a'; } else { auto it = umap.begin(); ans += (it->first); } } cout << ans; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; char s[maxn]; char ans[maxn]; bool fix[maxn]; int main() { int n; scanf("%d", &n); memset(ans, '?', sizeof(ans)); memset(fix, false, sizeof(fix)); int len; for (int i = 0; i < n; ++i) { scanf("%s", s); len = strlen(s); for (int i = 0; i < len; ++i) { if (s[i] != '?') { if (ans[i] == '?' && !fix[i]) { ans[i] = s[i]; continue; } if (ans[i] == s[i]) continue; ans[i] = '?'; fix[i] = true; } } } for (int i = 0; i < len; ++i) { if (!fix[i] && ans[i] == '?') ans[i] = 'a'; } ans[len] = 0; printf("%s\n", ans); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; char s[maxn]; char ans[maxn]; bool fix[maxn]; int main() { int n; scanf("%d", &n); memset(ans, '?', sizeof(ans)); memset(fix, false, sizeof(fix)); int len; for (int i = 0; i < n; ++i) { scanf("%s", s); len = strlen(s); for (int i = 0; i < len; ++i) { if (s[i] != '?') { if (ans[i] == '?' && !fix[i]) { ans[i] = s[i]; continue; } if (ans[i] == s[i]) continue; ans[i] = '?'; fix[i] = true; } } } for (int i = 0; i < len; ++i) { if (!fix[i] && ans[i] == '?') ans[i] = 'a'; } ans[len] = 0; printf("%s\n", ans); return 0; } ```
#include <bits/stdc++.h> const int N = 100050; int f[N][27]; int main() { int n; scanf("%d", &n); int m = 0; while (n--) { char c = getchar(); for (; (c < 'a' || c > 'z') && c != '?'; c = getchar()) ; int i = 1; for (; (c >= 'a' && c <= 'z') || c == '?'; c = getchar(), i++) { if (c == '?') f[i][26] = 1; else f[i][c - 'a'] = 1; } m = i - 1; } for (int i = 1; i <= m; i++) { int k = 0; for (int j = 0; j <= 26; j++) if (f[i][j]) k++; if (k >= 3) printf("?"); else if (k == 1) { if (f[i][26]) printf("a"); else { for (int j = 0; j <= 25; j++) if (f[i][j]) printf("%c", 'a' + j); } } else { if (!f[i][26]) printf("?"); else { for (int j = 0; j <= 25; j++) if (f[i][j]) printf("%c", 'a' + j); } } } }
### Prompt Create a solution in CPP for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const int N = 100050; int f[N][27]; int main() { int n; scanf("%d", &n); int m = 0; while (n--) { char c = getchar(); for (; (c < 'a' || c > 'z') && c != '?'; c = getchar()) ; int i = 1; for (; (c >= 'a' && c <= 'z') || c == '?'; c = getchar(), i++) { if (c == '?') f[i][26] = 1; else f[i][c - 'a'] = 1; } m = i - 1; } for (int i = 1; i <= m; i++) { int k = 0; for (int j = 0; j <= 26; j++) if (f[i][j]) k++; if (k >= 3) printf("?"); else if (k == 1) { if (f[i][26]) printf("a"); else { for (int j = 0; j <= 25; j++) if (f[i][j]) printf("%c", 'a' + j); } } else { if (!f[i][26]) printf("?"); else { for (int j = 0; j <= 25; j++) if (f[i][j]) printf("%c", 'a' + j); } } } } ```
#include <bits/stdc++.h> const int MAX_LEN = 100005; using namespace std; char ans[MAX_LEN]; int main() { ios::sync_with_stdio(false); int stringc, length; cin >> stringc; for (int i = 0; i < stringc; i++) { string cur; cin >> cur; length = cur.size(); for (int j = 0; j < (int)cur.size(); j++) { if (cur[j] != '?') { if (cur[j] != ans[j]) { if (ans[j] == 0) { ans[j] = cur[j]; } else { ans[j] = '?'; } } } } } for (int i = 0; i < length; i++) { cout << (ans[i] == 0 ? 'a' : ans[i]); } cout << endl; }
### Prompt Create a solution in cpp for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const int MAX_LEN = 100005; using namespace std; char ans[MAX_LEN]; int main() { ios::sync_with_stdio(false); int stringc, length; cin >> stringc; for (int i = 0; i < stringc; i++) { string cur; cin >> cur; length = cur.size(); for (int j = 0; j < (int)cur.size(); j++) { if (cur[j] != '?') { if (cur[j] != ans[j]) { if (ans[j] == 0) { ans[j] = cur[j]; } else { ans[j] = '?'; } } } } } for (int i = 0; i < length; i++) { cout << (ans[i] == 0 ? 'a' : ans[i]); } cout << endl; } ```
#include <bits/stdc++.h> using namespace std; set<char> S; int main() { int n; scanf("%d", &n); string s; vector<string> a; int i, j, m; for (i = 0; i < n; i++) { cin >> s; m = s.size(); a.push_back(s); } string ans = ""; for (j = 0; j < m; j++) { S.clear(); int d = 0; char c = 'a'; for (i = 0; i < n; i++) { if (a[i][j] == '?') { continue; } c = a[i][j]; if (S.find(a[i][j]) == S.end()) { S.insert(a[i][j]); d++; } } if (d >= 2) { ans += '?'; } else { ans += c; } } cout << ans; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; set<char> S; int main() { int n; scanf("%d", &n); string s; vector<string> a; int i, j, m; for (i = 0; i < n; i++) { cin >> s; m = s.size(); a.push_back(s); } string ans = ""; for (j = 0; j < m; j++) { S.clear(); int d = 0; char c = 'a'; for (i = 0; i < n; i++) { if (a[i][j] == '?') { continue; } c = a[i][j]; if (S.find(a[i][j]) == S.end()) { S.insert(a[i][j]); d++; } } if (d >= 2) { ans += '?'; } else { ans += c; } } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char letter[26]; int judge[maxn]; int main() { int n; char str[maxn]; letter[0] = 'a'; for (int i = 1; i < 26; i++) letter[i] = letter[i - 1] + 1; while (~scanf("%d", &n)) { int flag = 0, len; memset(judge, 0, sizeof(judge)); while (n--) { cin >> str; len = strlen(str); if (flag == 0) { for (int i = 0; i < len; i++) { judge[i] = (int)(str[i] - 'a'); } flag = 1; } else if (flag) { for (int i = 0; i < len; i++) { int tmp = (int)(str[i] - 'a'); if (tmp != -34) { if (tmp != judge[i] && judge[i] != -34) judge[i] = -1; else if (judge[i] == -34) judge[i] = tmp; } } } } for (int i = 0; i < len; i++) { if (judge[i] == -34) printf("a"); else if (judge[i] == -1) printf("?"); else printf("%c", letter[judge[i]]); } printf("\n"); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char letter[26]; int judge[maxn]; int main() { int n; char str[maxn]; letter[0] = 'a'; for (int i = 1; i < 26; i++) letter[i] = letter[i - 1] + 1; while (~scanf("%d", &n)) { int flag = 0, len; memset(judge, 0, sizeof(judge)); while (n--) { cin >> str; len = strlen(str); if (flag == 0) { for (int i = 0; i < len; i++) { judge[i] = (int)(str[i] - 'a'); } flag = 1; } else if (flag) { for (int i = 0; i < len; i++) { int tmp = (int)(str[i] - 'a'); if (tmp != -34) { if (tmp != judge[i] && judge[i] != -34) judge[i] = -1; else if (judge[i] == -34) judge[i] = tmp; } } } } for (int i = 0; i < len; i++) { if (judge[i] == -34) printf("a"); else if (judge[i] == -1) printf("?"); else printf("%c", letter[judge[i]]); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[100005], final[100005]; int main(void) { int n, len, i, j; while (scanf("%d", &n) != EOF) { scanf("%s", final); len = strlen(final); for (i = 2; i <= n; i++) { scanf("%s", s); for (j = 0; j < len; j++) { if (final[j] == '!') { continue; } if (final[j] == '?' && s[j] == '?') { continue; } if (final[j] == '?') { final[j] = s[j]; continue; } if (s[j] == '?') { continue; } if (final[j] != s[j]) final[j] = '!'; } } for (i = 0; i < len; i++) { if (final[i] == '?') { printf("a"); continue; } if (final[i] == '!') { printf("?"); continue; } printf("%c", final[i]); } printf("\n"); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[100005], final[100005]; int main(void) { int n, len, i, j; while (scanf("%d", &n) != EOF) { scanf("%s", final); len = strlen(final); for (i = 2; i <= n; i++) { scanf("%s", s); for (j = 0; j < len; j++) { if (final[j] == '!') { continue; } if (final[j] == '?' && s[j] == '?') { continue; } if (final[j] == '?') { final[j] = s[j]; continue; } if (s[j] == '?') { continue; } if (final[j] != s[j]) final[j] = '!'; } } for (i = 0; i < len; i++) { if (final[i] == '?') { printf("a"); continue; } if (final[i] == '!') { printf("?"); continue; } printf("%c", final[i]); } printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; string s, ingresada; cin >> n >> s; long long size = s.size(); char string_final[size + 1]; if (n == 1) { for (int i = 0; i < size; i++) { if (s[i] == '?') { s[i] = 'a'; } } cout << s << endl; } else { for (int i = 0; i < size + 1; i++) { if (s[i] == '?') { string_final[i] = '!'; } else { string_final[i] = s[i]; } } for (int i = 0; i < n - 1; i++) { cin >> ingresada; for (int j = 0; j < size + 1; j++) { if (string_final[j] != ingresada[j] && string_final[j] != '!') { if (ingresada[j] != '?') { string_final[j] = '?'; } } else { if (string_final[j] == '!' && ingresada[j] != '?') { string_final[j] = ingresada[j]; } } } } for (int i = 0; i < size; i++) { if (string_final[i] == '!') { string_final[i] = 'a'; } } cout << string_final << endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; string s, ingresada; cin >> n >> s; long long size = s.size(); char string_final[size + 1]; if (n == 1) { for (int i = 0; i < size; i++) { if (s[i] == '?') { s[i] = 'a'; } } cout << s << endl; } else { for (int i = 0; i < size + 1; i++) { if (s[i] == '?') { string_final[i] = '!'; } else { string_final[i] = s[i]; } } for (int i = 0; i < n - 1; i++) { cin >> ingresada; for (int j = 0; j < size + 1; j++) { if (string_final[j] != ingresada[j] && string_final[j] != '!') { if (ingresada[j] != '?') { string_final[j] = '?'; } } else { if (string_final[j] == '!' && ingresada[j] != '?') { string_final[j] = ingresada[j]; } } } } for (int i = 0; i < size; i++) { if (string_final[i] == '!') { string_final[i] = 'a'; } } cout << string_final << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char a[1000000], b[1000000]; int n; cin >> n >> a; int l = strlen(a); for (int i = 1; i < n; i++) { cin >> b; for (int j = 0; j < l; j++) { if (a[j] == '?') a[j] = b[j]; else if (b[j] == '?') continue; else if (a[j] != b[j]) a[j] = '0'; } } for (int i = 0; i < l; i++) { if (a[i] == '0') a[i] = '?'; else if (a[i] == '?') a[i] = 'x'; } cout << a; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char a[1000000], b[1000000]; int n; cin >> n >> a; int l = strlen(a); for (int i = 1; i < n; i++) { cin >> b; for (int j = 0; j < l; j++) { if (a[j] == '?') a[j] = b[j]; else if (b[j] == '?') continue; else if (a[j] != b[j]) a[j] = '0'; } } for (int i = 0; i < l; i++) { if (a[i] == '0') a[i] = '?'; else if (a[i] == '?') a[i] = 'x'; } cout << a; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char s[100005]; char p[100005]; int i, j, n, l; scanf("%d", &n); for (i = 0; i < n; i++) s[i] = 0; scanf("%s", p); l = strlen(p); for (i = 0; i < l; i++) s[i] = 0; for (i = 0; i < n; i++) { for (j = 0; j < l; j++) { if (p[j] != '?') { if (s[j] == 0) s[j] = p[j]; else if (s[j] == '?') ; else if (s[j] != p[j]) s[j] = '?'; } } scanf("%s", p); } for (i = 0; i < l; i++) { if (s[i] == 0) s[i] = 'x'; } s[l] = '\0'; printf("%s", s); return (0); }
### Prompt Generate a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char s[100005]; char p[100005]; int i, j, n, l; scanf("%d", &n); for (i = 0; i < n; i++) s[i] = 0; scanf("%s", p); l = strlen(p); for (i = 0; i < l; i++) s[i] = 0; for (i = 0; i < n; i++) { for (j = 0; j < l; j++) { if (p[j] != '?') { if (s[j] == 0) s[j] = p[j]; else if (s[j] == '?') ; else if (s[j] != p[j]) s[j] = '?'; } } scanf("%s", p); } for (i = 0; i < l; i++) { if (s[i] == 0) s[i] = 'x'; } s[l] = '\0'; printf("%s", s); return (0); } ```
#include <bits/stdc++.h> using namespace std; int F[26]; int main() { int n; while (cin >> n) { string s; vector<string> V; for (int i = 0; i < n; i++) cin >> s, V.push_back(s); string ans; for (int j = 0; j < V[0].size(); j++) { memset(F, 0, sizeof F); for (int i = 0; i < n; i++) { if (V[i][j] != '?') F[V[i][j] - 'a']++; } int ct = 0, pos; for (int i = 0; i < 26; i++) if (F[i] > 0) ct++, pos = i; if (ct > 1) ans.push_back('?'); else { if (ct == 0) ans.push_back('a'); else ans.push_back(char('a' + pos)); } } cout << ans << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int F[26]; int main() { int n; while (cin >> n) { string s; vector<string> V; for (int i = 0; i < n; i++) cin >> s, V.push_back(s); string ans; for (int j = 0; j < V[0].size(); j++) { memset(F, 0, sizeof F); for (int i = 0; i < n; i++) { if (V[i][j] != '?') F[V[i][j] - 'a']++; } int ct = 0, pos; for (int i = 0; i < 26; i++) if (F[i] > 0) ct++, pos = i; if (ct > 1) ans.push_back('?'); else { if (ct == 0) ans.push_back('a'); else ans.push_back(char('a' + pos)); } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1234567; char ans[N], foo[N]; int main() { int n; scanf("%d", &n); memset(ans, '*', sizeof(ans)); int len; for (int i = 0; i < n; i++) { scanf("%s", foo); len = strlen(foo); for (int j = 0; foo[j]; j++) if (foo[j] != '?') { if (ans[j] == '*') ans[j] = foo[j]; else if (ans[j] != foo[j]) ans[j] = '?'; } } for (int j = 0; j < len; j++) putchar(ans[j] == '*' ? 'a' : ans[j]); return 0; }
### Prompt Create a solution in Cpp for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1234567; char ans[N], foo[N]; int main() { int n; scanf("%d", &n); memset(ans, '*', sizeof(ans)); int len; for (int i = 0; i < n; i++) { scanf("%s", foo); len = strlen(foo); for (int j = 0; foo[j]; j++) if (foo[j] != '?') { if (ans[j] == '*') ans[j] = foo[j]; else if (ans[j] != foo[j]) ans[j] = '?'; } } for (int j = 0; j < len; j++) putchar(ans[j] == '*' ? 'a' : ans[j]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int sz = s.size(); char c; for (int i = 1; i < n; ++i) { for (int j = 0; j < sz; ++j) { cin >> c; if (c == '?') continue; if (s[j] == '?') s[j] = c; else if (s[j] != c) s[j] = 'X'; } } for (int i = 0; i < sz; ++i) { if (s[i] == '?') printf("c"); else if (s[i] == 'X') printf("?"); else printf("%c", s[i]); } printf("\n"); return 0; }
### Prompt Please create a solution in CPP to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int sz = s.size(); char c; for (int i = 1; i < n; ++i) { for (int j = 0; j < sz; ++j) { cin >> c; if (c == '?') continue; if (s[j] == '?') s[j] = c; else if (s[j] != c) s[j] = 'X'; } } for (int i = 0; i < sz; ++i) { if (s[i] == '?') printf("c"); else if (s[i] == 'X') printf("?"); else printf("%c", s[i]); } printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; string s, ans; int n, m; vector<char> st[maxn]; int main() { cin.tie(0), ios_base ::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> s; m = s.size(); for (int j = 0; j < m; j++) st[j].push_back(s[j]); } ans = s; for (int j = 0; j < m; j++) { sort(st[j].begin(), st[j].end()); st[j].resize(unique(st[j].begin(), st[j].end()) - st[j].begin()); if (st[j].size() == 1) { if (st[j][0] == '?') ans[j] = 'a'; } else if (st[j].size() == 2) { if (st[j][0] == '?') ans[j] = st[j][1]; else ans[j] = '?'; } else { ans[j] = '?'; } } cout << ans; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; string s, ans; int n, m; vector<char> st[maxn]; int main() { cin.tie(0), ios_base ::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> s; m = s.size(); for (int j = 0; j < m; j++) st[j].push_back(s[j]); } ans = s; for (int j = 0; j < m; j++) { sort(st[j].begin(), st[j].end()); st[j].resize(unique(st[j].begin(), st[j].end()) - st[j].begin()); if (st[j].size() == 1) { if (st[j][0] == '?') ans[j] = 'a'; } else if (st[j].size() == 2) { if (st[j][0] == '?') ans[j] = st[j][1]; else ans[j] = '?'; } else { ans[j] = '?'; } } cout << ans; return 0; } ```
#include <bits/stdc++.h> const int maxn = 100100; char ch[maxn], ans[maxn]; int next[maxn]; int main() { int n, len; scanf("%d", &n); for (int i = 0; i < maxn; i++) { next[i] = i + 1; ans[i] = -1; } int f = 1; next[maxn - 1] = 0; while (n--) { scanf("%s", ch); if (f == 1) { f++; len = strlen(ch); } next[len - 1] = -1; ans[len] = 0; int front = maxn - 1, t = next[maxn - 1]; if (t == -1) break; while (t != -1) { bool flag = false; if (ans[t] != ch[t]) { if (ans[t] == -1 && ch[t] != '?') ans[t] = ch[t]; else if (ch[t] != '?') { ans[t] = '?'; next[front] = next[t]; flag = true; } } if (!flag) front = t; t = next[t]; } } while (ans[++n] != 0) if (ans[n] == -1) ans[n] = 'a'; printf("%s\n", ans); return 0; }
### Prompt Generate a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const int maxn = 100100; char ch[maxn], ans[maxn]; int next[maxn]; int main() { int n, len; scanf("%d", &n); for (int i = 0; i < maxn; i++) { next[i] = i + 1; ans[i] = -1; } int f = 1; next[maxn - 1] = 0; while (n--) { scanf("%s", ch); if (f == 1) { f++; len = strlen(ch); } next[len - 1] = -1; ans[len] = 0; int front = maxn - 1, t = next[maxn - 1]; if (t == -1) break; while (t != -1) { bool flag = false; if (ans[t] != ch[t]) { if (ans[t] == -1 && ch[t] != '?') ans[t] = ch[t]; else if (ch[t] != '?') { ans[t] = '?'; next[front] = next[t]; flag = true; } } if (!flag) front = t; t = next[t]; } } while (ans[++n] != 0) if (ans[n] == -1) ans[n] = 'a'; printf("%s\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string a, b; int n; cin >> n >> a; for (int j = 1; j < n; j++) { cin >> b; for (int i = 0; i < a.size(); i++) { if (a[i] == '?') { a[i] = b[i]; } if (a[i] == '.') { continue; } else { if (a[i] != b[i] && b[i] != '?') { a[i] = '.'; } } } } for (int i = 0; i < a.size(); i++) { if (a[i] == '.') { cout << '?'; } else { if (a[i] == '?') { cout << 'a'; } else { cout << a[i]; } } } cout << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string a, b; int n; cin >> n >> a; for (int j = 1; j < n; j++) { cin >> b; for (int i = 0; i < a.size(); i++) { if (a[i] == '?') { a[i] = b[i]; } if (a[i] == '.') { continue; } else { if (a[i] != b[i] && b[i] != '?') { a[i] = '.'; } } } } for (int i = 0; i < a.size(); i++) { if (a[i] == '.') { cout << '?'; } else { if (a[i] == '?') { cout << 'a'; } else { cout << a[i]; } } } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string a[n + 1]; for (long long i = 0; i < n; i++) cin >> a[i]; long long len = a[0].size(); string res = ""; for (long long i = 0; i < len; i++) { map<char, long long> mp; for (long long j = 0; j < n; j++) { mp[a[j][i]]++; } long long ans = 0, ans1 = 0; char vis[200] = {0}, ch; for (auto x : mp) { if (x.first == '?') ans++; else { if (vis[x.first] == 0) { ans1++; vis[x.first] = 1; ch = x.first; } } } if (ans1 == 0) { res += 'a'; } else if (ans1 == 1) { res += ch; } else res += '?'; } cout << res << endl; }
### Prompt In CPP, your task is to solve the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string a[n + 1]; for (long long i = 0; i < n; i++) cin >> a[i]; long long len = a[0].size(); string res = ""; for (long long i = 0; i < len; i++) { map<char, long long> mp; for (long long j = 0; j < n; j++) { mp[a[j][i]]++; } long long ans = 0, ans1 = 0; char vis[200] = {0}, ch; for (auto x : mp) { if (x.first == '?') ans++; else { if (vis[x.first] == 0) { ans1++; vis[x.first] = 1; ch = x.first; } } } if (ans1 == 0) { res += 'a'; } else if (ans1 == 1) { res += ch; } else res += '?'; } cout << res << endl; } ```
#include <bits/stdc++.h> using namespace std; int n; int k = 0, t, q = 0; string s[100010]; char ch, zx; set<char> myset; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; t = (int)s[1].size(); for (int j = 0; j < t; j++) { for (int i = 1; i <= n; i++) { zx = s[i][j]; myset.insert(zx); if (s[i][j] == '?') k++; else { ch = s[i][j]; } } if (k == n) cout << 'x'; else if ((int)myset.size() == 2 && k > 0) cout << ch; else if ((int)myset.size() == 2 && k == 0) cout << '?'; else if (k == 0 && (int)myset.size() == 1) cout << ch; else cout << '?'; k = 0; myset.clear(); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int k = 0, t, q = 0; string s[100010]; char ch, zx; set<char> myset; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; t = (int)s[1].size(); for (int j = 0; j < t; j++) { for (int i = 1; i <= n; i++) { zx = s[i][j]; myset.insert(zx); if (s[i][j] == '?') k++; else { ch = s[i][j]; } } if (k == n) cout << 'x'; else if ((int)myset.size() == 2 && k > 0) cout << ch; else if ((int)myset.size() == 2 && k == 0) cout << '?'; else if (k == 0 && (int)myset.size() == 1) cout << ch; else cout << '?'; k = 0; myset.clear(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200000; int n, len; char ans[N], foo[N]; int main() { scanf("%d", &n); memset(ans, '*', sizeof ans); for (int i = 0; i < n; i++) { scanf("%s", foo); len = strlen(foo); for (int j = 0; j < len; j++) { if (foo[j] != '?') { if (ans[j] == '*') { ans[j] = foo[j]; } else if (ans[j] != foo[j]) { ans[j] = '?'; } } } } for (int j = 0; j < len; j++) { putchar(ans[j] == '*' ? 'a' : ans[j]); } return 0; }
### Prompt In CPP, your task is to solve the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200000; int n, len; char ans[N], foo[N]; int main() { scanf("%d", &n); memset(ans, '*', sizeof ans); for (int i = 0; i < n; i++) { scanf("%s", foo); len = strlen(foo); for (int j = 0; j < len; j++) { if (foo[j] != '?') { if (ans[j] == '*') { ans[j] = foo[j]; } else if (ans[j] != foo[j]) { ans[j] = '?'; } } } } for (int j = 0; j < len; j++) { putchar(ans[j] == '*' ? 'a' : ans[j]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; char a[100041]; int b[100041]; int c[100041]; string s; int main() { int n; cin >> n; int len; for (long long i = 0; i < 100003; i++) a[i] = '?', c[i] = 1; for (long long l = 0; l < n; l++) { cin >> s; len = s.size(); for (long long i = 0; i < s.size(); i++) { if (s[i] == '?') { continue; } if (c[i]) { c[i] = 0; a[i] = s[i]; } else { if (a[i] != s[i]) b[i] = 1; } } } for (long long i = 0; i < len; i++) { if (b[i]) cout << '?'; else if (c[i]) cout << 'x'; else cout << a[i]; } }
### Prompt Construct a CPP code solution to the problem outlined: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char a[100041]; int b[100041]; int c[100041]; string s; int main() { int n; cin >> n; int len; for (long long i = 0; i < 100003; i++) a[i] = '?', c[i] = 1; for (long long l = 0; l < n; l++) { cin >> s; len = s.size(); for (long long i = 0; i < s.size(); i++) { if (s[i] == '?') { continue; } if (c[i]) { c[i] = 0; a[i] = s[i]; } else { if (a[i] != s[i]) b[i] = 1; } } } for (long long i = 0; i < len; i++) { if (b[i]) cout << '?'; else if (c[i]) cout << 'x'; else cout << a[i]; } } ```
#include <bits/stdc++.h> using namespace std; long n; char s[100100]; map<char, bool> mymap[100100]; void input() { long i, j, len, sz; scanf("%ld", &n); for (i = 0; i < n; i++) { scanf("%s", &s); for (j = 0; s[j]; j++) if (s[j] != '?') mymap[j][s[j]] = true; } for (i = 0; s[i]; i++) { sz = mymap[i].size(); if (sz == 0) s[i] = 'a'; else if (sz == 1) s[i] = mymap[i].begin()->first; else s[i] = '?'; } printf("%s\n", s); } int main() { input(); return 0; }
### Prompt Create a solution in cpp for the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long n; char s[100100]; map<char, bool> mymap[100100]; void input() { long i, j, len, sz; scanf("%ld", &n); for (i = 0; i < n; i++) { scanf("%s", &s); for (j = 0; s[j]; j++) if (s[j] != '?') mymap[j][s[j]] = true; } for (i = 0; s[i]; i++) { sz = mymap[i].size(); if (sz == 0) s[i] = 'a'; else if (sz == 1) s[i] = mymap[i].begin()->first; else s[i] = '?'; } printf("%s\n", s); } int main() { input(); return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; int n; int bad[1 << 17]; char res[1 << 17]; char buf[1 << 17]; int main() { memset(bad, 0, sizeof(bad)); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", buf); if (i == 0) memcpy(res, buf, sizeof(res)); else { for (int j = 0; buf[j]; ++j) { if (res[j] != '?') { if (buf[j] != '?' && buf[j] != res[j]) bad[j] = 1; } else res[j] = buf[j]; } } } for (int i = 0; res[i]; ++i) { if (bad[i]) printf("?"); else { if (res[i] == '?') res[i] = 'a'; printf("%c", res[i]); } } printf("\n"); return 0; }
### Prompt In CPP, your task is to solve the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; int n; int bad[1 << 17]; char res[1 << 17]; char buf[1 << 17]; int main() { memset(bad, 0, sizeof(bad)); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", buf); if (i == 0) memcpy(res, buf, sizeof(res)); else { for (int j = 0; buf[j]; ++j) { if (res[j] != '?') { if (buf[j] != '?' && buf[j] != res[j]) bad[j] = 1; } else res[j] = buf[j]; } } } for (int i = 0; res[i]; ++i) { if (bad[i]) printf("?"); else { if (res[i] == '?') res[i] = 'a'; printf("%c", res[i]); } } printf("\n"); return 0; } ```
#include <bits/stdc++.h> const int M = 1e5 + 10; char str[M], c, n[M]; int m; int main() { scanf("%d", &m); getchar(); memset(n, 0, sizeof(n)); int len; while (m--) { len = 0; while (c = getchar()) { if (c == '\n') break; if (n[len] == 0 && c != '?') n[len] = c; else if (n[len] >= 'a' && n[len] <= 'z' && n[len] != c && c != '?') n[len] = '?'; len++; } } for (int i = 0; i < len; i++) if (n[i] == 0) n[i] = 'x'; printf("%s", n); printf("\n"); }
### Prompt Construct a Cpp code solution to the problem outlined: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const int M = 1e5 + 10; char str[M], c, n[M]; int m; int main() { scanf("%d", &m); getchar(); memset(n, 0, sizeof(n)); int len; while (m--) { len = 0; while (c = getchar()) { if (c == '\n') break; if (n[len] == 0 && c != '?') n[len] = c; else if (n[len] >= 'a' && n[len] <= 'z' && n[len] != c && c != '?') n[len] = '?'; len++; } } for (int i = 0; i < len; i++) if (n[i] == 0) n[i] = 'x'; printf("%s", n); printf("\n"); } ```
#include <bits/stdc++.h> using namespace std; int n, len = 0, flen = 0; string a[100001]; char temp[100001], ans[100001]; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", &temp); a[i] = temp; len = a[i].size(); if (len > flen) flen = len; for (int j = 0; j < len; j++) { if (ans[j] == '?') continue; else if (a[i][j] == '?') continue; else if (ans[j] == 0) ans[j] = a[i][j]; else if (ans[j] == a[i][j]) continue; else ans[j] = '?'; } } for (int i = 0; i < flen; i++) if (ans[i] > 0) printf("%c", ans[i]); else printf("a"); } int main() { init(); return 0; }
### Prompt In CPP, your task is to solve the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, len = 0, flen = 0; string a[100001]; char temp[100001], ans[100001]; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", &temp); a[i] = temp; len = a[i].size(); if (len > flen) flen = len; for (int j = 0; j < len; j++) { if (ans[j] == '?') continue; else if (a[i][j] == '?') continue; else if (ans[j] == 0) ans[j] = a[i][j]; else if (ans[j] == a[i][j]) continue; else ans[j] = '?'; } } for (int i = 0; i < flen; i++) if (ans[i] > 0) printf("%c", ans[i]); else printf("a"); } int main() { init(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long modpow(long long x, unsigned long long y) { long long res = 1; x = x % M; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % M; y = y >> 1; x = (x * x) % M; } return res; } int main() { ios_base::sync_with_stdio(false); ; cin.tie(NULL); ; int n; cin >> n; vector<string> v; for (int i = 0; i < n; i++) { string tmp; cin >> tmp; v.push_back(tmp); } string ans; int len = v[0].size(); if (n == 1) { for (int i = 0; i < len; i++) { if (v[0][i] == '?') { ans += 'a'; } else { ans += v[0][i]; } } cout << ans << endl; return 0; } set<int> s; for (int i = 0; i < len; i++) { s.clear(); bool doit = true; for (int j = 0; j < n; j++) { s.insert(v[j][i]); if (s.size() > 2) { ans += '?'; break; } if (s.size() == 2) { int ter = *s.begin(); for (auto it = s.begin(); it != s.end(); it++) { if (*it != ter && *it != '?' && ter != '?') { doit = false; break; } } } } if (s.size() == 2 && !doit) { ans += '?'; } if (s.size() == 2 && doit) { for (auto it = s.begin(); it != s.end(); it++) { if (*it != '?') { ans += *it; break; } } } if (s.size() == 1 && *s.begin() == '?') { ans += 'a'; } else if (s.size() == 1 && *s.begin() != '?') { ans += *s.begin(); } } cout << ans << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long modpow(long long x, unsigned long long y) { long long res = 1; x = x % M; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % M; y = y >> 1; x = (x * x) % M; } return res; } int main() { ios_base::sync_with_stdio(false); ; cin.tie(NULL); ; int n; cin >> n; vector<string> v; for (int i = 0; i < n; i++) { string tmp; cin >> tmp; v.push_back(tmp); } string ans; int len = v[0].size(); if (n == 1) { for (int i = 0; i < len; i++) { if (v[0][i] == '?') { ans += 'a'; } else { ans += v[0][i]; } } cout << ans << endl; return 0; } set<int> s; for (int i = 0; i < len; i++) { s.clear(); bool doit = true; for (int j = 0; j < n; j++) { s.insert(v[j][i]); if (s.size() > 2) { ans += '?'; break; } if (s.size() == 2) { int ter = *s.begin(); for (auto it = s.begin(); it != s.end(); it++) { if (*it != ter && *it != '?' && ter != '?') { doit = false; break; } } } } if (s.size() == 2 && !doit) { ans += '?'; } if (s.size() == 2 && doit) { for (auto it = s.begin(); it != s.end(); it++) { if (*it != '?') { ans += *it; break; } } } if (s.size() == 1 && *s.begin() == '?') { ans += 'a'; } else if (s.size() == 1 && *s.begin() != '?') { ans += *s.begin(); } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int j = 0; j < n - 1; j++) { string t; cin >> t; for (int i = 0; i < t.length(); i++) if (t[i] != '?' && t[i] != s[i]) if (s[i] == '?') s[i] = t[i]; else s[i] = '!'; } for (int i = 0; i < s.length(); i++) { if (s[i] == '?') s[i] = 'a'; if (s[i] == '!') s[i] = '?'; } cout << s; }
### Prompt In cpp, your task is to solve the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int j = 0; j < n - 1; j++) { string t; cin >> t; for (int i = 0; i < t.length(); i++) if (t[i] != '?' && t[i] != s[i]) if (s[i] == '?') s[i] = t[i]; else s[i] = '!'; } for (int i = 0; i < s.length(); i++) { if (s[i] == '?') s[i] = 'a'; if (s[i] == '!') s[i] = '?'; } cout << s; } ```
#include <bits/stdc++.h> const double eps = 1e-7; const int inf = 0x7FFFFFFF; using namespace std; char buf[100010]; char idx[100010], ans[100010]; int main() { int n, l; cin >> n; for (int i = 0; i < n; i++) { scanf("%s", buf); l = strlen(buf); if (!i) { for (int j = 0; j < l; j++) idx[j] = buf[j]; continue; } for (int j = 0; j < l; j++) { if (buf[j] != idx[j]) { if (idx[j] == '?') idx[j] = buf[j]; else if (buf[j] != '?') ans[j] = '?'; } } } for (int i = 0; i < l; i++) if (ans[i] != '?') { if (idx[i] != '?') ans[i] = idx[i]; else ans[i] = 'x'; } ans[l] = 0; printf("%s\n", ans); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> const double eps = 1e-7; const int inf = 0x7FFFFFFF; using namespace std; char buf[100010]; char idx[100010], ans[100010]; int main() { int n, l; cin >> n; for (int i = 0; i < n; i++) { scanf("%s", buf); l = strlen(buf); if (!i) { for (int j = 0; j < l; j++) idx[j] = buf[j]; continue; } for (int j = 0; j < l; j++) { if (buf[j] != idx[j]) { if (idx[j] == '?') idx[j] = buf[j]; else if (buf[j] != '?') ans[j] = '?'; } } } for (int i = 0; i < l; i++) if (ans[i] != '?') { if (idx[i] != '?') ans[i] = idx[i]; else ans[i] = 'x'; } ans[l] = 0; printf("%s\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<string> v(n); for (string &i : v) cin >> i; vector<char> at(v[0].length(), '.'); for (string i : v) { for (long long j = 0; j < i.length(); ++j) { if (i[j] >= 'a' && i[j] <= 'z') { if (at[j] == '.' || at[j] == i[j]) at[j] = i[j]; else at[j] = '?'; } } } for (char i : at) if (i == '.') cout << "a"; else cout << i; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<string> v(n); for (string &i : v) cin >> i; vector<char> at(v[0].length(), '.'); for (string i : v) { for (long long j = 0; j < i.length(); ++j) { if (i[j] >= 'a' && i[j] <= 'z') { if (at[j] == '.' || at[j] == i[j]) at[j] = i[j]; else at[j] = '?'; } } } for (char i : at) if (i == '.') cout << "a"; else cout << i; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int i = 1; i < n; i++) { string ss; cin >> ss; for (int j = 0; j < s.length(); j++) if (ss[j] != '?' && ss[j] != s[j]) if (s[j] == '?') s[j] = ss[j]; else s[j] = '!'; } for (int i = 0; i < s.length(); i++) if (s[i] == '?') s[i] = 'a'; else if (s[i] == '!') s[i] = '?'; cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int i = 1; i < n; i++) { string ss; cin >> ss; for (int j = 0; j < s.length(); j++) if (ss[j] != '?' && ss[j] != s[j]) if (s[j] == '?') s[j] = ss[j]; else s[j] = '!'; } for (int i = 0; i < s.length(); i++) if (s[i] == '?') s[i] = 'a'; else if (s[i] == '!') s[i] = '?'; cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; char a[100000]; int main() { int n; cin >> n; for (int i = 0; i < 100000; i++) { a[i] = '?'; } string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < s.size(); j++) { if (a[j] == '?' && s[j] != '?') { a[j] = s[j]; } else if (a[j] != s[j] && s[j] != '?') { a[j] = '!'; } } } for (int i = 0; i < s.size(); i++) { if (a[i] == '?') { cout << "a"; } else if (a[i] == '!') { cout << "?"; } else { cout << a[i]; } } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char a[100000]; int main() { int n; cin >> n; for (int i = 0; i < 100000; i++) { a[i] = '?'; } string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < s.size(); j++) { if (a[j] == '?' && s[j] != '?') { a[j] = s[j]; } else if (a[j] != s[j] && s[j] != '?') { a[j] = '!'; } } } for (int i = 0; i < s.size(); i++) { if (a[i] == '?') { cout << "a"; } else if (a[i] == '!') { cout << "?"; } else { cout << a[i]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, len; cin >> n; char cur[100100]; for (int i = 0; i < 100000; i++) cur[i] = '0'; for (int i = 0; i < n; i++) { string ss; cin >> ss; len = ss.size(); for (int j = 0; j < ss.size(); j++) { if (cur[j] == '0' && ss[j] != '?') cur[j] = ss[j]; if (cur[j] != '0' && ss[j] != '?' && cur[j] != ss[j]) cur[j] = '?'; } } for (int i = 0; i < len; i++) if (cur[i] == '0') cur[i] = 'a'; for (int i = 0; i < len; i++) cout << cur[i]; }
### Prompt Please formulate a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, len; cin >> n; char cur[100100]; for (int i = 0; i < 100000; i++) cur[i] = '0'; for (int i = 0; i < n; i++) { string ss; cin >> ss; len = ss.size(); for (int j = 0; j < ss.size(); j++) { if (cur[j] == '0' && ss[j] != '?') cur[j] = ss[j]; if (cur[j] != '0' && ss[j] != '?' && cur[j] != ss[j]) cur[j] = '?'; } } for (int i = 0; i < len; i++) if (cur[i] == '0') cur[i] = 'a'; for (int i = 0; i < len; i++) cout << cur[i]; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, ans, cnt; char c[N]; char k; string a[N]; bool flag; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < a[0].size(); i++) { k = 'a'; flag = false; for (int j = 0; j < n; j++) if (a[j][i] != '?') { if (flag && a[j][i] != k) { k = '?'; break; } else k = a[j][i]; flag = true; } cout << k; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, ans, cnt; char c[N]; char k; string a[N]; bool flag; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < a[0].size(); i++) { k = 'a'; flag = false; for (int j = 0; j < n; j++) if (a[j][i] != '?') { if (flag && a[j][i] != k) { k = '?'; break; } else k = a[j][i]; flag = true; } cout << k; } return 0; } ```
#include <bits/stdc++.h> using namespace std; char ch[100010], temp[100010]; bool ok[100010]; int main() { int n; cin >> n; cin >> ch; int m = strlen(ch); for (int i = 1; i < n; i++) { cin >> temp; for (int j = 0; j < m; j++) { if (ch[j] >= 'a' && ch[j] <= 'z' && temp[j] >= 'a' && temp[j] <= 'z' && ch[j] != temp[j]) ok[j] = 1, ch[j] = '?'; if (ch[j] == '?' && !ok[j]) ch[j] = temp[j]; } } for (int i = 0; i < m; i++) { if (ch[i] == '?' && !ok[i]) cout << "x"; else cout << ch[i]; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char ch[100010], temp[100010]; bool ok[100010]; int main() { int n; cin >> n; cin >> ch; int m = strlen(ch); for (int i = 1; i < n; i++) { cin >> temp; for (int j = 0; j < m; j++) { if (ch[j] >= 'a' && ch[j] <= 'z' && temp[j] >= 'a' && temp[j] <= 'z' && ch[j] != temp[j]) ok[j] = 1, ch[j] = '?'; if (ch[j] == '?' && !ok[j]) ch[j] = temp[j]; } } for (int i = 0; i < m; i++) { if (ch[i] == '?' && !ok[i]) cout << "x"; else cout << ch[i]; } return 0; } ```
#include <bits/stdc++.h> using namespace std; set<int> s[100001]; int main() { int n, i, l, k; string a; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> a; for (l = 0; l < a.size(); l++) { if (a[l] != '?') s[l].insert(a[l]); } k = a.size(); } for (i = 0; i < k; i++) { if (s[i].size() == 0) printf("a"); else { if (s[i].size() == 1) printf("%c", *s[i].begin()); else printf("?"); } } printf("\n"); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; set<int> s[100001]; int main() { int n, i, l, k; string a; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> a; for (l = 0; l < a.size(); l++) { if (a[l] != '?') s[l].insert(a[l]); } k = a.size(); } for (i = 0; i < k; i++) { if (s[i].size() == 0) printf("a"); else { if (s[i].size() == 1) printf("%c", *s[i].begin()); else printf("?"); } } printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)(1e5 + 20); const long long int INF = (1ll << 62); int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; int m = v[0].length(); for (int i = 0; i < m; ++i) { int last = 0; for (int j = 0; j < n; ++j) { if (v[j][i] != '?') { if (last && last != v[j][i]) { last = N; break; } last = v[j][i]; } } if (last == N) cout << '?'; else if (last == 0) cout << 'a'; else cout << (char)(last); } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)(1e5 + 20); const long long int INF = (1ll << 62); int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; int m = v[0].length(); for (int i = 0; i < m; ++i) { int last = 0; for (int j = 0; j < n; ++j) { if (v[j][i] != '?') { if (last && last != v[j][i]) { last = N; break; } last = v[j][i]; } } if (last == N) cout << '?'; else if (last == 0) cout << 'a'; else cout << (char)(last); } return 0; } ```
#include <bits/stdc++.h> using namespace std; char ar[100005]; int main() { int n; cin >> n; for (int i = 0; i < 100005; ++i) ar[i] = '?'; int ln; int t = n; while (n--) { string str; cin >> str; ln = str.length(); for (int i = 0; i < str.length(); ++i) { if (ar[i] == '?' && str[i] != '?') ar[i] = str[i]; else if (ar[i] != '?' && str[i] != '?' && ar[i] != str[i]) { ar[i] = '*'; } } } for (int i = 0; i < ln; ++i) { if (ar[i] == '*') cout << '?'; else if (ar[i] == '?') cout << 'a'; else cout << ar[i]; } cout << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char ar[100005]; int main() { int n; cin >> n; for (int i = 0; i < 100005; ++i) ar[i] = '?'; int ln; int t = n; while (n--) { string str; cin >> str; ln = str.length(); for (int i = 0; i < str.length(); ++i) { if (ar[i] == '?' && str[i] != '?') ar[i] = str[i]; else if (ar[i] != '?' && str[i] != '?' && ar[i] != str[i]) { ar[i] = '*'; } } } for (int i = 0; i < ln; ++i) { if (ar[i] == '*') cout << '?'; else if (ar[i] == '?') cout << 'a'; else cout << ar[i]; } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; char abc[100005]; int main() { ios::sync_with_stdio(false); cin >> n; memset(abc, 0, sizeof(abc)); string str; for (int i = 0; i < n; i++) { cin >> str; for (size_t j = 0; j < str.size(); j++) { if (abc[j] == 0 && str[j] != '?') abc[j] = str[j]; else if (abc[j] > 1 && str[j] != '?' && abc[j] != str[j]) abc[j] = 1; } } for (size_t j = 0; j < str.size(); j++) { if (abc[j] == 0) cout << 'a'; else if (abc[j] == 1) cout << '?'; else cout << abc[j]; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; char abc[100005]; int main() { ios::sync_with_stdio(false); cin >> n; memset(abc, 0, sizeof(abc)); string str; for (int i = 0; i < n; i++) { cin >> str; for (size_t j = 0; j < str.size(); j++) { if (abc[j] == 0 && str[j] != '?') abc[j] = str[j]; else if (abc[j] > 1 && str[j] != '?' && abc[j] != str[j]) abc[j] = 1; } } for (size_t j = 0; j < str.size(); j++) { if (abc[j] == 0) cout << 'a'; else if (abc[j] == 1) cout << '?'; else cout << abc[j]; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int MIN(int a, int b) { int x = (b > a) ? a : b; return x; } int MAX(int a, int b) { int x = (b < a) ? a : b; return x; } const int size = 1000007; const long long modulo = 1000000007; const long long inf = 1e18; const double eps = 1e-6; long long n, m, a[4000], b[4000], k, n25, n50, n100; int f[1000]; int pos[1000]; bool xx[1000000]; char str[100009]; int main() { char temp[100009]; char ss; while (scanf("%d", &n) == 1) { scanf("%s", temp); int N = strlen(temp); for (int i = 0; i < N; i++) { if (temp[i] == '?') str[i] = '~'; else str[i] = temp[i]; } for (int j = 1; j < n; j++) { scanf("%s", temp); for (int i = 0; i < N; i++) { if (temp[i] == '?') { } else if (str[i] != temp[i]) { if (str[i] == '~') str[i] = temp[i]; else str[i] = '?'; } } } for (int i = 0; i < N; i++) if (str[i] == '~') str[i] = 'a'; str[N] = 0; printf("%s\n", str); } return 0; }
### Prompt Generate a CPP solution to the following problem: Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β€” the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int MIN(int a, int b) { int x = (b > a) ? a : b; return x; } int MAX(int a, int b) { int x = (b < a) ? a : b; return x; } const int size = 1000007; const long long modulo = 1000000007; const long long inf = 1e18; const double eps = 1e-6; long long n, m, a[4000], b[4000], k, n25, n50, n100; int f[1000]; int pos[1000]; bool xx[1000000]; char str[100009]; int main() { char temp[100009]; char ss; while (scanf("%d", &n) == 1) { scanf("%s", temp); int N = strlen(temp); for (int i = 0; i < N; i++) { if (temp[i] == '?') str[i] = '~'; else str[i] = temp[i]; } for (int j = 1; j < n; j++) { scanf("%s", temp); for (int i = 0; i < N; i++) { if (temp[i] == '?') { } else if (str[i] != temp[i]) { if (str[i] == '~') str[i] = temp[i]; else str[i] = '?'; } } } for (int i = 0; i < N; i++) if (str[i] == '~') str[i] = 'a'; str[N] = 0; printf("%s\n", str); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9 + 7; const int MXN = (int)1e5 + 7; int n, k, t; int fac[MXN], rev[MXN], m[MXN]; int binpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = (1LL * res * x) % INF; y >>= 1; x = (1LL * x * x) % INF; } return res; } int c(int x, int y) { if (y > x) return 0; long long res = (1LL * fac[x] * rev[x - y]) % INF; res = (1LL * res * rev[y]) % INF; return res; } int f(int d) { int res = c(n / d - 1, k - 1); return res; } int main() { fac[0] = 1; rev[0] = 1; for (int i = 1; i < MXN; i++) fac[i] = (fac[i - 1] * 1LL * i) % INF; for (int i = 1; i < MXN; i++) { m[i] = 1; int cur = i; for (int d = 2; d * d <= cur; d++) { if (cur % d != 0) continue; int cnt = 0; while (cur % d == 0) { cur /= d; cnt++; } if (cnt >= 2) { m[i] = 0; break; } m[i] = -m[i]; } if (cur > 1) m[i] = -m[i]; rev[i] = binpow(fac[i], INF - 2); } scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); int ans = 0; for (int d = 1; d * d <= n; d++) { if (n % d != 0) continue; ans = (ans + 1LL * m[d] * f(d)) % INF; if (n / d != d) ans = (ans + 1LL * m[n / d] * f(n / d)) % INF; if (ans < 0) ans += INF; } printf("%d\n", ans); } return 0; }
### Prompt Please create a solution in CPP to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = (int)1e9 + 7; const int MXN = (int)1e5 + 7; int n, k, t; int fac[MXN], rev[MXN], m[MXN]; int binpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = (1LL * res * x) % INF; y >>= 1; x = (1LL * x * x) % INF; } return res; } int c(int x, int y) { if (y > x) return 0; long long res = (1LL * fac[x] * rev[x - y]) % INF; res = (1LL * res * rev[y]) % INF; return res; } int f(int d) { int res = c(n / d - 1, k - 1); return res; } int main() { fac[0] = 1; rev[0] = 1; for (int i = 1; i < MXN; i++) fac[i] = (fac[i - 1] * 1LL * i) % INF; for (int i = 1; i < MXN; i++) { m[i] = 1; int cur = i; for (int d = 2; d * d <= cur; d++) { if (cur % d != 0) continue; int cnt = 0; while (cur % d == 0) { cur /= d; cnt++; } if (cnt >= 2) { m[i] = 0; break; } m[i] = -m[i]; } if (cur > 1) m[i] = -m[i]; rev[i] = binpow(fac[i], INF - 2); } scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); int ans = 0; for (int d = 1; d * d <= n; d++) { if (n % d != 0) continue; ans = (ans + 1LL * m[d] * f(d)) % INF; if (n / d != d) ans = (ans + 1LL * m[n / d] * f(n / d)) % INF; if (ans < 0) ans += INF; } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int f[300005], inv[300005]; int bigmod(int a, int n) { if (n == 0) return 1; int ans = bigmod(a, n / 2); ans = (1LL * ans * ans) % 1000000007; if (n % 2) { ans = (1LL * ans * a) % 1000000007; } return ans; } void pre() { f[0] = 1; inv[0] = 1; for (int i = 1; i < 300005; i++) { f[i] = 1LL * f[i - 1] * i % 1000000007; inv[i] = bigmod(f[i], 1000000007 - 2); } } int C(int n, int r) { if (n < r) return 0; return 1LL * f[n] * inv[r] % 1000000007 * inv[n - r] % 1000000007; } unordered_map<int, int, custom_hash> mp; vector<int> all[300005]; map<pair<int, int>, int> cc; bool comp[300005]; int k; int run(int n) { if (cc.count({n, k})) return cc[{n, k}]; if (mp.count(n)) return mp[n]; int ans = C(n - 1, k - 1); for (int u : all[n]) { if (n / u >= k) ans = (ans - run(n / u)) % 1000000007; ans = (ans + 1000000007) % 1000000007; } cc[{n, k}] = ans; return mp[n] = ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; for (int i = 2; i < 300005; i++) { { for (int j = i * 2; j < 300005; j += i) { comp[j] = 1; all[j].push_back(i); } } } pre(); int q; cin >> q; while (q--) { int n; cin >> n >> k; if (k == 1) { cout << (n == 1) << '\n'; continue; } mp.clear(); cout << run(n) << '\n'; } }
### Prompt Generate a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int f[300005], inv[300005]; int bigmod(int a, int n) { if (n == 0) return 1; int ans = bigmod(a, n / 2); ans = (1LL * ans * ans) % 1000000007; if (n % 2) { ans = (1LL * ans * a) % 1000000007; } return ans; } void pre() { f[0] = 1; inv[0] = 1; for (int i = 1; i < 300005; i++) { f[i] = 1LL * f[i - 1] * i % 1000000007; inv[i] = bigmod(f[i], 1000000007 - 2); } } int C(int n, int r) { if (n < r) return 0; return 1LL * f[n] * inv[r] % 1000000007 * inv[n - r] % 1000000007; } unordered_map<int, int, custom_hash> mp; vector<int> all[300005]; map<pair<int, int>, int> cc; bool comp[300005]; int k; int run(int n) { if (cc.count({n, k})) return cc[{n, k}]; if (mp.count(n)) return mp[n]; int ans = C(n - 1, k - 1); for (int u : all[n]) { if (n / u >= k) ans = (ans - run(n / u)) % 1000000007; ans = (ans + 1000000007) % 1000000007; } cc[{n, k}] = ans; return mp[n] = ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; for (int i = 2; i < 300005; i++) { { for (int j = i * 2; j < 300005; j += i) { comp[j] = 1; all[j].push_back(i); } } } pre(); int q; cin >> q; while (q--) { int n; cin >> n >> k; if (k == 1) { cout << (n == 1) << '\n'; continue; } mp.clear(); cout << run(n) << '\n'; } } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks") #pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx") using namespace std; const long long inf = 0x7f7f7f7f7f7f; long long read() { long long x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } const long long N = 2e5 + 10; const long long mod = 1e9 + 7; struct qss { long long n, f, id; inline bool operator<(const qss a) const { return f < a.f; } } qs[N]; struct node { long long sum, len; } dp[N]; long long F[N], Finv[N], inv[N]; long long pri[N], min_pridiv[N], tot_pri; long long res[N]; void init() { inv[1] = 1; for (long long i = 2; i < N; i++) inv[i] = 1ll * (mod - mod / i) * 1ll * inv[mod % i] % mod; F[0] = Finv[0] = 1; for (long long i = 1; i < N; i++) { F[i] = 1ll * F[i - 1] * 1ll * i % mod; Finv[i] = 1ll * Finv[i - 1] * 1ll * inv[i] % mod; } pri[0] = 1; min_pridiv[1] = 0; for (long long i = 2; i < N; i++) { if (!min_pridiv[i]) { pri[++tot_pri] = i; min_pridiv[i] = tot_pri; } for (long long j = 1; j <= tot_pri && i * pri[j] < N; j++) { min_pridiv[i * pri[j]] = j; if (i % pri[j] == 0) break; } } } inline long long C(long long n, long long m) { if (m < 0 || m > n) return 0; return 1ll * F[n] * 1ll * Finv[m] % mod * Finv[n - m] % mod; } long long dfs(long long n, long long f) { if (n < f) { dp[n] = (node){0, f}; return 0; } if (dp[n].len == f) return dp[n].sum; if (n == pri[min_pridiv[n]]) { dp[n] = (node){C(n - 1, f - 1), f}; return dp[n].sum; } long long ret = C(n - 1, f - 1); for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret -= dfs(n / i, f); ret %= mod; if (i * i == n) break; ret -= dfs(n / (n / i), f); ret %= mod; } } ret = (ret + mod) % mod; dp[n] = (node){ret, f}; return ret; } signed main() { init(); long long q = read(); for (long long i = 1; i <= q; i++) { long long n = read(), f = read(); qs[i] = (qss){n, f, i}; } sort(qs + 1, qs + q + 1); for (long long i = 1; i <= q; i++) { if (qs[i].f == 1) res[qs[i].id] = (qs[i].n == 1) ? 1 : 0; else res[qs[i].id] = dfs(qs[i].n, qs[i].f); } for (long long i = 1; i <= q; i++) printf("%lld\n", res[i]); }
### Prompt Your task is to create a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks") #pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx") using namespace std; const long long inf = 0x7f7f7f7f7f7f; long long read() { long long x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } const long long N = 2e5 + 10; const long long mod = 1e9 + 7; struct qss { long long n, f, id; inline bool operator<(const qss a) const { return f < a.f; } } qs[N]; struct node { long long sum, len; } dp[N]; long long F[N], Finv[N], inv[N]; long long pri[N], min_pridiv[N], tot_pri; long long res[N]; void init() { inv[1] = 1; for (long long i = 2; i < N; i++) inv[i] = 1ll * (mod - mod / i) * 1ll * inv[mod % i] % mod; F[0] = Finv[0] = 1; for (long long i = 1; i < N; i++) { F[i] = 1ll * F[i - 1] * 1ll * i % mod; Finv[i] = 1ll * Finv[i - 1] * 1ll * inv[i] % mod; } pri[0] = 1; min_pridiv[1] = 0; for (long long i = 2; i < N; i++) { if (!min_pridiv[i]) { pri[++tot_pri] = i; min_pridiv[i] = tot_pri; } for (long long j = 1; j <= tot_pri && i * pri[j] < N; j++) { min_pridiv[i * pri[j]] = j; if (i % pri[j] == 0) break; } } } inline long long C(long long n, long long m) { if (m < 0 || m > n) return 0; return 1ll * F[n] * 1ll * Finv[m] % mod * Finv[n - m] % mod; } long long dfs(long long n, long long f) { if (n < f) { dp[n] = (node){0, f}; return 0; } if (dp[n].len == f) return dp[n].sum; if (n == pri[min_pridiv[n]]) { dp[n] = (node){C(n - 1, f - 1), f}; return dp[n].sum; } long long ret = C(n - 1, f - 1); for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret -= dfs(n / i, f); ret %= mod; if (i * i == n) break; ret -= dfs(n / (n / i), f); ret %= mod; } } ret = (ret + mod) % mod; dp[n] = (node){ret, f}; return ret; } signed main() { init(); long long q = read(); for (long long i = 1; i <= q; i++) { long long n = read(), f = read(); qs[i] = (qss){n, f, i}; } sort(qs + 1, qs + q + 1); for (long long i = 1; i <= q; i++) { if (qs[i].f == 1) res[qs[i].id] = (qs[i].n == 1) ? 1 : 0; else res[qs[i].id] = dfs(qs[i].n, qs[i].f); } for (long long i = 1; i <= q; i++) printf("%lld\n", res[i]); } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") const double pi = acos(-1); const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const int MAXN = 1e5 + 5; const double eps = 1e-9; using namespace std; long long fac[MAXN], inv[MAXN]; int sieve[MAXN], cnt[MAXN], fl[MAXN]; long long bin(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; b /= 2ll; } return ret; } long long ncr(long long n, long long k) { if (n < k) return 0; return (fac[n] * inv[n - k] % MOD) * inv[k] % MOD; } int main() { for (int i = 2; i < MAXN; i++) { if (!sieve[i]) for (int j = i; j < MAXN; j += i) sieve[j] = i; int x = i; fl[x] = 1; while (x > 1) { int cur = sieve[x]; while (sieve[x] == cur) { x /= sieve[x]; if (sieve[x] == cur) fl[i] = 0; } cnt[i]++; } } fac[0] = 1; for (long long i = 1; i < MAXN; i++) fac[i] = fac[i - 1] * i % MOD; inv[0] = 1; for (long long i = 1; i < MAXN; i++) inv[i] = bin(fac[i], MOD - 2); int q; scanf("%d", &(q)); for (int i = 0; i < q; i++) { int n, f; scanf("%d", &(n)), scanf("%d", &(f)); long long pr = ncr(n - 1, f - 1); for (long long x = 2; x * x <= n && f > 1; x++) { if (n % x != 0) continue; if (fl[x]) { if (cnt[x] & 1) pr = (pr - ncr(n / x - 1, f - 1) + MOD) % MOD; else pr = (pr + ncr(n / x - 1, f - 1)) % MOD; } if (x != n / x && fl[n / x]) { if (cnt[n / x] & 1) pr = (pr - ncr(x - 1, f - 1) + MOD) % MOD; else pr = (pr + ncr(x - 1, f - 1)) % MOD; } } if (f == 1 && n != 1) pr = 0; printf("%I64d\n", (pr)); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") const double pi = acos(-1); const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const int MAXN = 1e5 + 5; const double eps = 1e-9; using namespace std; long long fac[MAXN], inv[MAXN]; int sieve[MAXN], cnt[MAXN], fl[MAXN]; long long bin(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; b /= 2ll; } return ret; } long long ncr(long long n, long long k) { if (n < k) return 0; return (fac[n] * inv[n - k] % MOD) * inv[k] % MOD; } int main() { for (int i = 2; i < MAXN; i++) { if (!sieve[i]) for (int j = i; j < MAXN; j += i) sieve[j] = i; int x = i; fl[x] = 1; while (x > 1) { int cur = sieve[x]; while (sieve[x] == cur) { x /= sieve[x]; if (sieve[x] == cur) fl[i] = 0; } cnt[i]++; } } fac[0] = 1; for (long long i = 1; i < MAXN; i++) fac[i] = fac[i - 1] * i % MOD; inv[0] = 1; for (long long i = 1; i < MAXN; i++) inv[i] = bin(fac[i], MOD - 2); int q; scanf("%d", &(q)); for (int i = 0; i < q; i++) { int n, f; scanf("%d", &(n)), scanf("%d", &(f)); long long pr = ncr(n - 1, f - 1); for (long long x = 2; x * x <= n && f > 1; x++) { if (n % x != 0) continue; if (fl[x]) { if (cnt[x] & 1) pr = (pr - ncr(n / x - 1, f - 1) + MOD) % MOD; else pr = (pr + ncr(n / x - 1, f - 1)) % MOD; } if (x != n / x && fl[n / x]) { if (cnt[n / x] & 1) pr = (pr - ncr(x - 1, f - 1) + MOD) % MOD; else pr = (pr + ncr(x - 1, f - 1)) % MOD; } } if (f == 1 && n != 1) pr = 0; printf("%I64d\n", (pr)); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 1e5 + 11; inline long long max(long long a, long long b) { return ((a > b) ? a : b); } inline long long min(long long a, long long b) { return ((a > b) ? b : a); } inline long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } inline vector<long long> read(int n) { vector<long long> v(n); for (int i = 0; i < v.size(); i++) cin >> v[i]; return v; } const int mod = 1e9 + 7; vector<long long> v[MAXN]; long long factorial[2 * MAXN], ans[MAXN], Inverse[2 * MAXN]; long long power(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long modInverse(long long a) { return power(a, mod - 2); } void init() { factorial[0] = 1; for (int i = 1; i < 2 * MAXN; i++) factorial[i] = factorial[i - 1] * i % mod; for (int i = 0; i < 2 * MAXN; i++) Inverse[i] = modInverse(factorial[i]); } long long binomial_coefficient(int n, int k) { return factorial[n] * Inverse[k] % mod * Inverse[n - k] % mod; } void solveforthiscase(const int& test) { for (int i = MAXN - 1; i >= 1; i--) { for (int j = i; j < MAXN; j += i) { v[j].push_back(i); } } int q; cin >> q; while (q--) { int n, f; cin >> n >> f; long long t = binomial_coefficient(n - 1, f - 1) % mod; for (auto x : v[n]) { long long val = n / x; val--; if (val < f - 1 || x == 1) continue; ans[x] += binomial_coefficient(val, f - 1) % mod; ans[x] %= mod; for (auto y : v[x]) { if (y == x || y == 1) continue; ans[y] = (ans[y] - ans[x] + mod) % mod; } t -= ans[x]; t += mod; t %= mod; } for (auto x : v[n]) ans[x] = 0; cout << t << '\n'; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); clock_t start, end; start = clock(); int test = 1; init(); for (int i = 1; i <= test; i++) solveforthiscase(i); end = clock(); long double t_t = (long double)(end - start) / (long double)(CLOCKS_PER_SEC); cerr << (long double)t_t * 1000 << " ms "; }
### Prompt In CPP, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 1e5 + 11; inline long long max(long long a, long long b) { return ((a > b) ? a : b); } inline long long min(long long a, long long b) { return ((a > b) ? b : a); } inline long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } inline vector<long long> read(int n) { vector<long long> v(n); for (int i = 0; i < v.size(); i++) cin >> v[i]; return v; } const int mod = 1e9 + 7; vector<long long> v[MAXN]; long long factorial[2 * MAXN], ans[MAXN], Inverse[2 * MAXN]; long long power(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long modInverse(long long a) { return power(a, mod - 2); } void init() { factorial[0] = 1; for (int i = 1; i < 2 * MAXN; i++) factorial[i] = factorial[i - 1] * i % mod; for (int i = 0; i < 2 * MAXN; i++) Inverse[i] = modInverse(factorial[i]); } long long binomial_coefficient(int n, int k) { return factorial[n] * Inverse[k] % mod * Inverse[n - k] % mod; } void solveforthiscase(const int& test) { for (int i = MAXN - 1; i >= 1; i--) { for (int j = i; j < MAXN; j += i) { v[j].push_back(i); } } int q; cin >> q; while (q--) { int n, f; cin >> n >> f; long long t = binomial_coefficient(n - 1, f - 1) % mod; for (auto x : v[n]) { long long val = n / x; val--; if (val < f - 1 || x == 1) continue; ans[x] += binomial_coefficient(val, f - 1) % mod; ans[x] %= mod; for (auto y : v[x]) { if (y == x || y == 1) continue; ans[y] = (ans[y] - ans[x] + mod) % mod; } t -= ans[x]; t += mod; t %= mod; } for (auto x : v[n]) ans[x] = 0; cout << t << '\n'; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); clock_t start, end; start = clock(); int test = 1; init(); for (int i = 1; i <= test; i++) solveforthiscase(i); end = clock(); long double t_t = (long double)(end - start) / (long double)(CLOCKS_PER_SEC); cerr << (long double)t_t * 1000 << " ms "; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } const long long maxn = 4e5 + 5; const long long maxm = maxn << 1; const long long mod = 1e9 + 7; typedef long long room[maxn]; long long n, f; long long vis[maxn], pr[maxn], tot, mu[maxn]; long long fac[maxn], inv[maxn]; void init(long long x) { mu[1] = 1; for (long long i = 2; i <= x; ++i) { if (!vis[i]) pr[++tot] = i, mu[i] = -1; for (long long j = 1, k; k = 1ll * pr[j] * i, j <= tot && k <= x; ++j) { vis[k] = 1; if (!(i % pr[j])) { mu[k] = 0; break; } mu[k] = -mu[i]; } } } long long ksm(long long x, long long mi) { long long res(1); while (mi) { if (mi & 1) res = 1ll * res * x % mod; mi >>= 1; x = 1ll * x * x % mod; } return res; } inline long long getmu(long long x) { return (mu[x] + mod) % mod; } long long C(long long a, long long b) { if (a < b) return 0; return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod; } const long long N = 1e5; inline void update(long long &x, long long y) { x = (x + y + mod) % mod; } signed main() { long long i, j, q; fac[0] = 1; for (i = 1; i <= N; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; inv[N] = ksm(fac[N], mod - 2); for (i = N - 1; ~i; --i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; init(N); r1(q); while (q--) { r1(n), r1(f); long long ans(0); for (long long i = 1; 1ll * i * i <= n; ++i) { if (!(n % i)) { update(ans, 1ll * C(i - 1, f - 1) * getmu(n / i) % mod); if (i * i != n) update(ans, 1ll * C(n / i - 1, f - 1) * getmu(i) % mod); } } printf("%lld\n", ans); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } const long long maxn = 4e5 + 5; const long long maxm = maxn << 1; const long long mod = 1e9 + 7; typedef long long room[maxn]; long long n, f; long long vis[maxn], pr[maxn], tot, mu[maxn]; long long fac[maxn], inv[maxn]; void init(long long x) { mu[1] = 1; for (long long i = 2; i <= x; ++i) { if (!vis[i]) pr[++tot] = i, mu[i] = -1; for (long long j = 1, k; k = 1ll * pr[j] * i, j <= tot && k <= x; ++j) { vis[k] = 1; if (!(i % pr[j])) { mu[k] = 0; break; } mu[k] = -mu[i]; } } } long long ksm(long long x, long long mi) { long long res(1); while (mi) { if (mi & 1) res = 1ll * res * x % mod; mi >>= 1; x = 1ll * x * x % mod; } return res; } inline long long getmu(long long x) { return (mu[x] + mod) % mod; } long long C(long long a, long long b) { if (a < b) return 0; return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod; } const long long N = 1e5; inline void update(long long &x, long long y) { x = (x + y + mod) % mod; } signed main() { long long i, j, q; fac[0] = 1; for (i = 1; i <= N; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; inv[N] = ksm(fac[N], mod - 2); for (i = N - 1; ~i; --i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; init(N); r1(q); while (q--) { r1(n), r1(f); long long ans(0); for (long long i = 1; 1ll * i * i <= n; ++i) { if (!(n % i)) { update(ans, 1ll * C(i - 1, f - 1) * getmu(n / i) % mod); if (i * i != n) update(ans, 1ll * C(n / i - 1, f - 1) * getmu(i) % mod); } } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long P[100005], Inv[100005]; long long f1(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % 1000000007; x = x * x % 1000000007; y >>= 1; } return res; } int prm[100005], Q[100005], mu[100005], pri; void Init() { P[0] = 1; for (int i = 1; i < 100005; i++) P[i] = P[i - 1] * i % 1000000007; Inv[0] = 1; for (int i = 1; i < 100005; i++) Inv[i] = f1(P[i], 1000000007 - 2); mu[1] = 1; for (int i = 2; i < 100005; i++) { if (!Q[i]) { prm[++pri] = i; mu[i] = -1; } for (int j = 1; prm[j] * i < 100005; j++) { Q[prm[j] * i] = 1; if (i % prm[j] == 0) { mu[prm[j] * i] = 0; break; } mu[prm[j] * i] = -mu[i]; } } } long long C(int x, int y) { if (x < y || y < 0) return 0; return P[x] * Inv[y] % 1000000007 * Inv[x - y] % 1000000007; } int n, k; int main() { Init(); int q; scanf("%d", &q); while (q--) { scanf("%d%d", &n, &k); if (n != 1 && k == 1) { puts("0"); continue; } long long ans = C(n - 1, k - 1); for (int i = 2; i * i <= n; i++) if (n % i == 0) { ans = (ans + C(n / i - 1, k - 1) * mu[i] + 1000000007) % 1000000007; if (i * i != n) ans = (ans + C(i - 1, k - 1) * mu[n / i] + 1000000007) % 1000000007; } printf("%lld\n", ans); } return 0; }
### Prompt Create a solution in Cpp for the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long P[100005], Inv[100005]; long long f1(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % 1000000007; x = x * x % 1000000007; y >>= 1; } return res; } int prm[100005], Q[100005], mu[100005], pri; void Init() { P[0] = 1; for (int i = 1; i < 100005; i++) P[i] = P[i - 1] * i % 1000000007; Inv[0] = 1; for (int i = 1; i < 100005; i++) Inv[i] = f1(P[i], 1000000007 - 2); mu[1] = 1; for (int i = 2; i < 100005; i++) { if (!Q[i]) { prm[++pri] = i; mu[i] = -1; } for (int j = 1; prm[j] * i < 100005; j++) { Q[prm[j] * i] = 1; if (i % prm[j] == 0) { mu[prm[j] * i] = 0; break; } mu[prm[j] * i] = -mu[i]; } } } long long C(int x, int y) { if (x < y || y < 0) return 0; return P[x] * Inv[y] % 1000000007 * Inv[x - y] % 1000000007; } int n, k; int main() { Init(); int q; scanf("%d", &q); while (q--) { scanf("%d%d", &n, &k); if (n != 1 && k == 1) { puts("0"); continue; } long long ans = C(n - 1, k - 1); for (int i = 2; i * i <= n; i++) if (n % i == 0) { ans = (ans + C(n / i - 1, k - 1) * mu[i] + 1000000007) % 1000000007; if (i * i != n) ans = (ans + C(i - 1, k - 1) * mu[n / i] + 1000000007) % 1000000007; } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long exp(long long b, long long e) { if (b == 1 || e == 0) return 1; if (e == 1) return b % 1000000007; if (e == 2) return (b * b) % 1000000007; return (((e & 1) == 0) ? exp(exp(b, e >> 1), 2) % 1000000007 : (b * (exp(exp(b, e >> 1), 2) % 1000000007)) % 1000000007); } long long gcd(long long a, long long b) { return ((b == 0) ? a : gcd(b, a % b)); } long long sp[100006], fact[100005], invf[100003]; void prime_factors(long long n, vector<int>& ret) { int prv = -1; while (n != 1) { if (sp[n] != prv) { ret.push_back(sp[n]); prv = sp[n]; } n /= sp[n]; } } int main() { sp[1] = 1; for (int i = 2; i < 100003; ++i) sp[i] = 2 - 3 * (i & 1); for (long long i = 3; i < 100003; i += 2) { if (sp[i] != -1) continue; sp[i] = i; for (long long j = i * i; j < 100003; j += i) { if (sp[j] == -1) sp[j] = i; } } fact[0] = invf[0] = 1; for (int i = 1; i < 100002; ++i) { fact[i] = (fact[i - 1] * i) % 1000000007; invf[i] = exp(fact[i], 1000000007 - 2); } int q; scanf("%d", &q); while (q--) { long long n, f, ans; scanf("%lld %lld", &n, &f); ans = (((fact[n - 1] * invf[n - f]) % 1000000007) * invf[f - 1]) % 1000000007; vector<int> pr; prime_factors(n, pr); long long sz = pr.size(); for (int i = 1; i < (1 << sz); ++i) { int j = i, cnt = 0, k = 0; long long num = n; while (j != 0) { if ((j & 1) == 1) { num /= pr[k]; ++cnt; } ++k; j = (j >> 1); } if (num < f) continue; if ((cnt & 1) == 1) ans = (ans - (((fact[num - 1] * invf[num - f]) % 1000000007) * invf[f - 1]) % 1000000007 + 1000000007) % 1000000007; else ans = (ans + (((fact[num - 1] * invf[num - f]) % 1000000007) * invf[f - 1]) % 1000000007) % 1000000007; } printf("%lld\n", ans); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long exp(long long b, long long e) { if (b == 1 || e == 0) return 1; if (e == 1) return b % 1000000007; if (e == 2) return (b * b) % 1000000007; return (((e & 1) == 0) ? exp(exp(b, e >> 1), 2) % 1000000007 : (b * (exp(exp(b, e >> 1), 2) % 1000000007)) % 1000000007); } long long gcd(long long a, long long b) { return ((b == 0) ? a : gcd(b, a % b)); } long long sp[100006], fact[100005], invf[100003]; void prime_factors(long long n, vector<int>& ret) { int prv = -1; while (n != 1) { if (sp[n] != prv) { ret.push_back(sp[n]); prv = sp[n]; } n /= sp[n]; } } int main() { sp[1] = 1; for (int i = 2; i < 100003; ++i) sp[i] = 2 - 3 * (i & 1); for (long long i = 3; i < 100003; i += 2) { if (sp[i] != -1) continue; sp[i] = i; for (long long j = i * i; j < 100003; j += i) { if (sp[j] == -1) sp[j] = i; } } fact[0] = invf[0] = 1; for (int i = 1; i < 100002; ++i) { fact[i] = (fact[i - 1] * i) % 1000000007; invf[i] = exp(fact[i], 1000000007 - 2); } int q; scanf("%d", &q); while (q--) { long long n, f, ans; scanf("%lld %lld", &n, &f); ans = (((fact[n - 1] * invf[n - f]) % 1000000007) * invf[f - 1]) % 1000000007; vector<int> pr; prime_factors(n, pr); long long sz = pr.size(); for (int i = 1; i < (1 << sz); ++i) { int j = i, cnt = 0, k = 0; long long num = n; while (j != 0) { if ((j & 1) == 1) { num /= pr[k]; ++cnt; } ++k; j = (j >> 1); } if (num < f) continue; if ((cnt & 1) == 1) ans = (ans - (((fact[num - 1] * invf[num - f]) % 1000000007) * invf[f - 1]) % 1000000007 + 1000000007) % 1000000007; else ans = (ans + (((fact[num - 1] * invf[num - f]) % 1000000007) * invf[f - 1]) % 1000000007) % 1000000007; } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> const int Max_N = 9 + (int)1e5; const long long mod = 7 + (int)1e9; int fac[Max_N], fac_inv[Max_N], inv[Max_N], clk, ans[Max_N], flag[Max_N]; std::vector<int> aaaa1111[Max_N]; void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } void init(int n) { fac_inv[0] = fac[0] = 1; for (int i = 1, j; i <= n; ++i) { for (j = i << 1; j < n; j += i) aaaa1111[j].push_back(i); if (i != 1) inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod, fac[i] = (long long)fac[i - 1] * i % mod; else inv[i] = fac[i] = 1; } fac_inv[1] = inv[1]; for (int i = 2; i <= n; ++i) { fac_inv[i] = (long long)fac_inv[i - 1] * inv[i] % mod; } } int C(int n, int m) { long long ret = fac[n]; ret = ret * fac_inv[m] % mod; ret = ret * fac_inv[n - m] % mod; return (int)ret; } int solve(int n, int m) { if (n < m) return 0; if (n == m) return 1; int &ret = ans[n]; if (flag[n] == clk) return ret; flag[n] = clk; ret = C(n - 1, m - 1); for (__typeof((aaaa1111[n]).begin()) it = (aaaa1111[n]).begin(); it != (aaaa1111[n]).end(); ++it) { ret -= solve(*it, m); if (ret < 0) ret += mod; } return ret; } int main() { init(Max_N - 5); int nq; scanf("%d", &nq); for (int n, m; nq--;) { scanf("%d%d", &n, &m); ++clk; printf("%d\n", solve(n, m)); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> const int Max_N = 9 + (int)1e5; const long long mod = 7 + (int)1e9; int fac[Max_N], fac_inv[Max_N], inv[Max_N], clk, ans[Max_N], flag[Max_N]; std::vector<int> aaaa1111[Max_N]; void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } void init(int n) { fac_inv[0] = fac[0] = 1; for (int i = 1, j; i <= n; ++i) { for (j = i << 1; j < n; j += i) aaaa1111[j].push_back(i); if (i != 1) inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod, fac[i] = (long long)fac[i - 1] * i % mod; else inv[i] = fac[i] = 1; } fac_inv[1] = inv[1]; for (int i = 2; i <= n; ++i) { fac_inv[i] = (long long)fac_inv[i - 1] * inv[i] % mod; } } int C(int n, int m) { long long ret = fac[n]; ret = ret * fac_inv[m] % mod; ret = ret * fac_inv[n - m] % mod; return (int)ret; } int solve(int n, int m) { if (n < m) return 0; if (n == m) return 1; int &ret = ans[n]; if (flag[n] == clk) return ret; flag[n] = clk; ret = C(n - 1, m - 1); for (__typeof((aaaa1111[n]).begin()) it = (aaaa1111[n]).begin(); it != (aaaa1111[n]).end(); ++it) { ret -= solve(*it, m); if (ret < 0) ret += mod; } return ret; } int main() { init(Max_N - 5); int nq; scanf("%d", &nq); for (int n, m; nq--;) { scanf("%d%d", &n, &m); ++clk; printf("%d\n", solve(n, m)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, mN = 1e5 + 100; int add(int a, int b) { a += b; if (a < 0) a += mod; if (a >= mod) a -= mod; return a; } int mul(int a, int b) { long long ret = 1LL * a * b; if (ret >= mod) ret %= mod; return ret; } int pwm(int a, long long p) { int m = a, ret = 1; while (p) { if (p & 1) ret = mul(ret, m); m = mul(m, m); p >>= 1; } return ret; } int fact[mN]; int nCr(int n, int r) { if (r > n || r < 0) return 0; return mul(mul(fact[n], pwm(fact[n - r], mod - 2)), pwm(fact[r], mod - 2)); } vector<int> primes; int n, f; void getprimes() { primes.clear(); int tmp = n; for (int i = 2; i * i <= n; ++i) { if (tmp % i == 0) primes.push_back(i); while (tmp % i == 0) tmp /= i; } if (tmp > 1) primes.push_back(tmp); } int out = 0; void solve(int idx, int v, int p) { if (idx == primes.size()) { if (p) out = add(out, -nCr(n / v - 1, f - 1)); else out = add(out, nCr(n / v - 1, f - 1)); return; } solve(idx + 1, v, p); solve(idx + 1, v * primes[idx], p ^ 1); } int main() { fact[0] = 1; for (int i = 1; i < mN; ++i) fact[i] = mul(fact[i - 1], i); int q; scanf("%d", &q); for (int i = 0; i < q; ++i) { scanf("%d", &n), scanf("%d", &f); out = 0; getprimes(); solve(0, 1, 0); printf("%d\n", out); } }
### Prompt In Cpp, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, mN = 1e5 + 100; int add(int a, int b) { a += b; if (a < 0) a += mod; if (a >= mod) a -= mod; return a; } int mul(int a, int b) { long long ret = 1LL * a * b; if (ret >= mod) ret %= mod; return ret; } int pwm(int a, long long p) { int m = a, ret = 1; while (p) { if (p & 1) ret = mul(ret, m); m = mul(m, m); p >>= 1; } return ret; } int fact[mN]; int nCr(int n, int r) { if (r > n || r < 0) return 0; return mul(mul(fact[n], pwm(fact[n - r], mod - 2)), pwm(fact[r], mod - 2)); } vector<int> primes; int n, f; void getprimes() { primes.clear(); int tmp = n; for (int i = 2; i * i <= n; ++i) { if (tmp % i == 0) primes.push_back(i); while (tmp % i == 0) tmp /= i; } if (tmp > 1) primes.push_back(tmp); } int out = 0; void solve(int idx, int v, int p) { if (idx == primes.size()) { if (p) out = add(out, -nCr(n / v - 1, f - 1)); else out = add(out, nCr(n / v - 1, f - 1)); return; } solve(idx + 1, v, p); solve(idx + 1, v * primes[idx], p ^ 1); } int main() { fact[0] = 1; for (int i = 1; i < mN; ++i) fact[i] = mul(fact[i - 1], i); int q; scanf("%d", &q); for (int i = 0; i < q; ++i) { scanf("%d", &n), scanf("%d", &f); out = 0; getprimes(); solve(0, 1, 0); printf("%d\n", out); } } ```
#include <bits/stdc++.h> using namespace std; constexpr int maxn = 1e5 + 4; constexpr long long mods = 1e9 + 7; int prm[maxn], pc = 0; int vis[maxn], mu[maxn]; long long fac[maxn]; inline long long modpow(long long a, int p = mods - 2) { long long res = 1; while (p) { if (p & 1) (res *= a) %= mods; (a *= a) %= mods; p >>= 1; } return res; } inline long long binom(int n, int m) { if (n < m) return 0; return fac[n] * modpow(fac[m]) % mods * modpow(fac[n - m]) % mods; } int N, M; inline long long f(int d) { return binom(d - 1, M - 1); } int main() { mu[1] = 1; fac[0] = 1; for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i % mods; for (int i = 2; i < maxn; ++i) { if (!vis[i]) { prm[++pc] = i; mu[i] = -1; } for (int j = 1; j <= pc && i * prm[j] < maxn; ++j) { vis[i * prm[j]] = j; if (i % prm[j] == 0) { mu[i * prm[j]] = 0; break; } mu[i * prm[j]] = -mu[i]; } } int Q; cin >> Q; while (Q--) { cin >> N >> M; long long ans = 0; for (int i = 1; i * i <= N; ++i) { if (N % i == 0) { (ans += mu[N / i] * f(i)) %= mods; if (N != i * i) (ans += mu[i] * f(N / i)) %= mods; } } cout << (ans + mods) % mods << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; constexpr int maxn = 1e5 + 4; constexpr long long mods = 1e9 + 7; int prm[maxn], pc = 0; int vis[maxn], mu[maxn]; long long fac[maxn]; inline long long modpow(long long a, int p = mods - 2) { long long res = 1; while (p) { if (p & 1) (res *= a) %= mods; (a *= a) %= mods; p >>= 1; } return res; } inline long long binom(int n, int m) { if (n < m) return 0; return fac[n] * modpow(fac[m]) % mods * modpow(fac[n - m]) % mods; } int N, M; inline long long f(int d) { return binom(d - 1, M - 1); } int main() { mu[1] = 1; fac[0] = 1; for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i % mods; for (int i = 2; i < maxn; ++i) { if (!vis[i]) { prm[++pc] = i; mu[i] = -1; } for (int j = 1; j <= pc && i * prm[j] < maxn; ++j) { vis[i * prm[j]] = j; if (i % prm[j] == 0) { mu[i * prm[j]] = 0; break; } mu[i * prm[j]] = -mu[i]; } } int Q; cin >> Q; while (Q--) { cin >> N >> M; long long ans = 0; for (int i = 1; i * i <= N; ++i) { if (N % i == 0) { (ans += mu[N / i] * f(i)) %= mods; if (N != i * i) (ans += mu[i] * f(N / i)) %= mods; } } cout << (ans + mods) % mods << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int lim = 1e5; int t, x, n, fac[100005], inv[100005]; long long res; int read() { int x = 0, fl = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fl = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * fl; } int qpow(int base, int pw) { int s = 1; while (pw) { if (pw & 1) s = 1ll * s * base % mod; base = 1ll * base * base % mod; pw >>= 1; } return s; } int C(int x, int y) { if (x < y) return 0; return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; } int mu(int x) { int p = 1; for (int i = 2; i * i <= x; i++) { if (x % i) continue; x /= i; p = -p; if (x % i == 0) return 0; } if (x > 1) p = -p; return p; } int main() { fac[0] = 1; for (int i = 1; i <= lim; i++) fac[i] = 1ll * fac[i - 1] * (long long)(i) % mod; inv[lim] = qpow(fac[lim], mod - 2); for (int i = lim - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (long long)(i + 1) % mod; t = read(); while (t--) { x = read(), n = read(), res = 0; for (int i = 1; i * i <= x; i++) { if (x % i) continue; res = (res + 1ll * mu(i) * C(x / i - 1, n - 1) % mod + mod) % mod; if (i * i != x) res = (res + 1ll * mu(x / i) * C(i - 1, n - 1) % mod + mod) % mod; } printf("%lld\n", res); } return 0; }
### Prompt In cpp, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int lim = 1e5; int t, x, n, fac[100005], inv[100005]; long long res; int read() { int x = 0, fl = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fl = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * fl; } int qpow(int base, int pw) { int s = 1; while (pw) { if (pw & 1) s = 1ll * s * base % mod; base = 1ll * base * base % mod; pw >>= 1; } return s; } int C(int x, int y) { if (x < y) return 0; return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; } int mu(int x) { int p = 1; for (int i = 2; i * i <= x; i++) { if (x % i) continue; x /= i; p = -p; if (x % i == 0) return 0; } if (x > 1) p = -p; return p; } int main() { fac[0] = 1; for (int i = 1; i <= lim; i++) fac[i] = 1ll * fac[i - 1] * (long long)(i) % mod; inv[lim] = qpow(fac[lim], mod - 2); for (int i = lim - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (long long)(i + 1) % mod; t = read(); while (t--) { x = read(), n = read(), res = 0; for (int i = 1; i * i <= x; i++) { if (x % i) continue; res = (res + 1ll * mu(i) * C(x / i - 1, n - 1) % mod + mod) % mod; if (i * i != x) res = (res + 1ll * mu(x / i) * C(i - 1, n - 1) % mod + mod) % mod; } printf("%lld\n", res); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long sz = 1e5 + 10, mod = 1e9 + 7; vector<long long> dv[sz]; long long dp[sz], fact[sz], inv[sz]; long long fastPow(long long x, long long n, long long MOD) { long long ret = 1; while (n) { if (n & 1) ret = (ret * x) % MOD; x = (x * x) % MOD; n >>= 1; } return ret % MOD; } inline long long starsBars(long long n, long long k) { n--, k--; long long den = (inv[k] * inv[n - k]) % mod; return (fact[n] * den) % mod; } long long solve(long long n, long long f) { if (n < f) return 0; if (dp[n] != -1) return dp[n]; long long way = starsBars(n, f); for (long long &g : dv[n]) { way -= solve(n / g, f); if (way < 0) way += mod; } return dp[n] = way; } int main() { for (long long i = 2; i < sz; i++) for (long long j = i + i; j < sz; j += i) dv[j].push_back(i); fact[0] = 1; for (long long i = 1; i <= sz - 1; ++i) fact[i] = (fact[i - 1] * i) % mod; inv[sz - 1] = fastPow(fact[sz - 1], mod - 2, mod); for (long long i = (sz - 1 - 1); i >= 0; --i) inv[i] = (inv[i + 1] * (i + 1)) % mod; long long q; cin >> q; while (q--) { long long n, f; scanf("%lld", &n), scanf("%lld", &f); if (f == 1) { if (n == 1) printf("1\n"); else printf("0\n"); continue; } for (long long &d : dv[n]) dp[d] = -1; dp[n] = -1; printf("%lld\n", solve(n, f)); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long sz = 1e5 + 10, mod = 1e9 + 7; vector<long long> dv[sz]; long long dp[sz], fact[sz], inv[sz]; long long fastPow(long long x, long long n, long long MOD) { long long ret = 1; while (n) { if (n & 1) ret = (ret * x) % MOD; x = (x * x) % MOD; n >>= 1; } return ret % MOD; } inline long long starsBars(long long n, long long k) { n--, k--; long long den = (inv[k] * inv[n - k]) % mod; return (fact[n] * den) % mod; } long long solve(long long n, long long f) { if (n < f) return 0; if (dp[n] != -1) return dp[n]; long long way = starsBars(n, f); for (long long &g : dv[n]) { way -= solve(n / g, f); if (way < 0) way += mod; } return dp[n] = way; } int main() { for (long long i = 2; i < sz; i++) for (long long j = i + i; j < sz; j += i) dv[j].push_back(i); fact[0] = 1; for (long long i = 1; i <= sz - 1; ++i) fact[i] = (fact[i - 1] * i) % mod; inv[sz - 1] = fastPow(fact[sz - 1], mod - 2, mod); for (long long i = (sz - 1 - 1); i >= 0; --i) inv[i] = (inv[i + 1] * (i + 1)) % mod; long long q; cin >> q; while (q--) { long long n, f; scanf("%lld", &n), scanf("%lld", &f); if (f == 1) { if (n == 1) printf("1\n"); else printf("0\n"); continue; } for (long long &d : dv[n]) dp[d] = -1; dp[n] = -1; printf("%lld\n", solve(n, f)); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") using namespace std; const long long N = 2e5 + 100, OO = 1e9 + 7, T = 1e6 + 10, M = 1e9 + 7, P = 6151, SQ = 280, lg = 20; long long fac[N], inv[N], flag[N]; long long pw(long long x, long long y) { if (y == 0) return 1; long long cnt = pw(x, y / 2); cnt = (cnt * cnt) % M; cnt = (cnt * (y % 2 == 1 ? x : 1)) % M; return cnt; } void prepro() { fac[0] = 1; for (long long i = 1; i <= N - lg; i++) fac[i] = (fac[i - 1] * i) % M; inv[N - lg] = pw(fac[N - lg], M - 2); for (long long i = N - lg - 1; i > -1; i--) inv[i] = (inv[i + 1] * (i + 1)) % M; } long long c(long long x, long long y) { if (x < y) return 0; if (x == y || y == 0) return 1; return ((fac[x] * inv[y] % M) * inv[x - y]) % M; } int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); prepro(); flag[1] = +1; for (long long i = 2; i <= 1e5; i++) { long long h = i, num = 0; bool u = true; for (long long j = 2; j * j <= h; j++) { long long k = 0; while (h % j == 0) k++, h /= j; if (k > 1) { u = false; break; } if (k == 1) num++; } if (!u) continue; if (h > 1) num++; if (num % 2) flag[i] = -1; else flag[i] = +1; } long long q; cin >> q; while (q--) { long long n, f; cin >> n >> f; if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; continue; } long long ans = 0; for (long long i = 1; i * i <= n; i++) { if (i * i == n) ans = (ans + flag[i] * c(i - 1, f - 1)) % M; else if (n % i == 0) ans = (ans + (flag[i] * c((n / i) - 1, f - 1) + flag[n / i] * c(i - 1, f - 1)) % M) % M; } if (ans < 0) ans += M; cout << ans << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") using namespace std; const long long N = 2e5 + 100, OO = 1e9 + 7, T = 1e6 + 10, M = 1e9 + 7, P = 6151, SQ = 280, lg = 20; long long fac[N], inv[N], flag[N]; long long pw(long long x, long long y) { if (y == 0) return 1; long long cnt = pw(x, y / 2); cnt = (cnt * cnt) % M; cnt = (cnt * (y % 2 == 1 ? x : 1)) % M; return cnt; } void prepro() { fac[0] = 1; for (long long i = 1; i <= N - lg; i++) fac[i] = (fac[i - 1] * i) % M; inv[N - lg] = pw(fac[N - lg], M - 2); for (long long i = N - lg - 1; i > -1; i--) inv[i] = (inv[i + 1] * (i + 1)) % M; } long long c(long long x, long long y) { if (x < y) return 0; if (x == y || y == 0) return 1; return ((fac[x] * inv[y] % M) * inv[x - y]) % M; } int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); prepro(); flag[1] = +1; for (long long i = 2; i <= 1e5; i++) { long long h = i, num = 0; bool u = true; for (long long j = 2; j * j <= h; j++) { long long k = 0; while (h % j == 0) k++, h /= j; if (k > 1) { u = false; break; } if (k == 1) num++; } if (!u) continue; if (h > 1) num++; if (num % 2) flag[i] = -1; else flag[i] = +1; } long long q; cin >> q; while (q--) { long long n, f; cin >> n >> f; if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; continue; } long long ans = 0; for (long long i = 1; i * i <= n; i++) { if (i * i == n) ans = (ans + flag[i] * c(i - 1, f - 1)) % M; else if (n % i == 0) ans = (ans + (flag[i] * c((n / i) - 1, f - 1) + flag[n / i] * c(i - 1, f - 1)) % M) % M; } if (ans < 0) ans += M; cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int MOD = 1e9 + 7; vector<int> vec; int fact[N], ive_fact[N]; int power(int x, int n) { int ans = 1; while (n) { if (n & 1) ans = (1ll * ans * x) % MOD; n /= 2; x = (1ll * x * x) % MOD; } return ans; } void init() { fact[0] = 1; for (int i = 1; i < N; i++) fact[i] = (1ll * fact[i - 1] * i) % MOD; for (int i = 0; i < N; i++) ive_fact[i] = power(fact[i], MOD - 2); } void divFactor(int cur) { vec.clear(); int tmp = sqrt(cur + 0.5); for (int i = 2; i <= tmp; i++) { if (cur % i == 0) { vec.push_back(i); while (cur % i == 0) cur /= i; } } if (cur != 1) vec.push_back(cur); } int solve(int n, int f) { if (n < f) return 0; int ans = fact[n]; ans = 1ll * ans * ive_fact[f] % MOD; ans = 1ll * ans * ive_fact[n - f] % MOD; return ans; } int cal(int n) { return (n % MOD + MOD) % MOD; } int main() { init(); int cas; scanf("%d", &cas); while (cas--) { int n, f; scanf("%d%d", &n, &f); divFactor(n); int ans = 0, t = 1 << vec.size(); for (int i = 0; i < t; i++) { int tmp = 1, sign = 1; for (int j = 0; j < vec.size(); j++) { if (i & (1 << j)) { sign *= -1; tmp *= vec[j]; } } ans = cal(ans + sign * solve(n / tmp - 1, f - 1)); } printf("%d\n", ans); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int MOD = 1e9 + 7; vector<int> vec; int fact[N], ive_fact[N]; int power(int x, int n) { int ans = 1; while (n) { if (n & 1) ans = (1ll * ans * x) % MOD; n /= 2; x = (1ll * x * x) % MOD; } return ans; } void init() { fact[0] = 1; for (int i = 1; i < N; i++) fact[i] = (1ll * fact[i - 1] * i) % MOD; for (int i = 0; i < N; i++) ive_fact[i] = power(fact[i], MOD - 2); } void divFactor(int cur) { vec.clear(); int tmp = sqrt(cur + 0.5); for (int i = 2; i <= tmp; i++) { if (cur % i == 0) { vec.push_back(i); while (cur % i == 0) cur /= i; } } if (cur != 1) vec.push_back(cur); } int solve(int n, int f) { if (n < f) return 0; int ans = fact[n]; ans = 1ll * ans * ive_fact[f] % MOD; ans = 1ll * ans * ive_fact[n - f] % MOD; return ans; } int cal(int n) { return (n % MOD + MOD) % MOD; } int main() { init(); int cas; scanf("%d", &cas); while (cas--) { int n, f; scanf("%d%d", &n, &f); divFactor(n); int ans = 0, t = 1 << vec.size(); for (int i = 0; i < t; i++) { int tmp = 1, sign = 1; for (int j = 0; j < vec.size(); j++) { if (i & (1 << j)) { sign *= -1; tmp *= vec[j]; } } ans = cal(ans + sign * solve(n / tmp - 1, f - 1)); } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; const int mod = 1e9 + 7; const int inf = INT_MAX; const int N = 1e5; ll modexp(ll x, ll n) { ll ans = 1; while (n) { if (n & 1) ans = ans * x % mod; x = x * x % mod; n /= 2; } return ans; } ll modinv(ll x) { return modexp(x, mod - 2); } ll fact[N + 1], inv[N + 1]; ll choose(int n, int k) { return fact[n] * inv[n - k] % mod * inv[k] % mod; } vector<int> factors[N + 1]; void comp() { fact[0] = inv[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = i * fact[i - 1] % mod; inv[i] = modinv(i) * inv[i - 1] % mod; } for (int i = 2; i <= N; i++) { if (factors[i].empty()) { for (int j = i; j <= N; j += i) { factors[j].push_back(i); } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); comp(); int q; cin >> q; while (q--) { int n, f; cin >> n >> f; int ans = 0; for (int mask = 0; mask < (1 << factors[n].size()); mask++) { int prod = 1; for (int i = 0; i < factors[n].size(); i++) { if (mask & (1 << i)) { prod *= factors[n][i]; } } if ((ll)f * prod <= n) { int dist = n / prod - f; if (__builtin_popcount(mask) & 1) { ans = (ans + mod - choose(f + dist - 1, f - 1)) % mod; } else ans = (ans + choose(f + dist - 1, f - 1)) % mod; } } cout << ans << '\n'; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; const int mod = 1e9 + 7; const int inf = INT_MAX; const int N = 1e5; ll modexp(ll x, ll n) { ll ans = 1; while (n) { if (n & 1) ans = ans * x % mod; x = x * x % mod; n /= 2; } return ans; } ll modinv(ll x) { return modexp(x, mod - 2); } ll fact[N + 1], inv[N + 1]; ll choose(int n, int k) { return fact[n] * inv[n - k] % mod * inv[k] % mod; } vector<int> factors[N + 1]; void comp() { fact[0] = inv[0] = 1; for (int i = 1; i <= N; i++) { fact[i] = i * fact[i - 1] % mod; inv[i] = modinv(i) * inv[i - 1] % mod; } for (int i = 2; i <= N; i++) { if (factors[i].empty()) { for (int j = i; j <= N; j += i) { factors[j].push_back(i); } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); comp(); int q; cin >> q; while (q--) { int n, f; cin >> n >> f; int ans = 0; for (int mask = 0; mask < (1 << factors[n].size()); mask++) { int prod = 1; for (int i = 0; i < factors[n].size(); i++) { if (mask & (1 << i)) { prod *= factors[n][i]; } } if ((ll)f * prod <= n) { int dist = n / prod - f; if (__builtin_popcount(mask) & 1) { ans = (ans + mod - choose(f + dist - 1, f - 1)) % mod; } else ans = (ans + choose(f + dist - 1, f - 1)) % mod; } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; template <class T> T gcd(T x, T y) { while (T t = x % y) x = y, y = t; return y; } const double eps = 1e-9; const double PI = acos(-1.); const int INF = 1000000000; const int MOD = 1000000007; const double E = 2.7182818284590452353602874713527; bool isdig(char x) { return x >= '0' && x <= '9'; } bool isup(char x) { return x >= 'A' && x <= 'Z'; } bool isdown(char x) { return x >= 'a' && x <= 'z'; } bool islet(char x) { return isup(x) || isdown(x); } int N, M; vector<int> fac[100005]; vector<int> ffac[100005]; bool del[100005]; long long jc[100005]; void pre() { jc[0] = jc[1] = 1; for (int i = 2; i <= 100000; i++) { jc[i] = jc[i - 1] * i % MOD; } for (int i = 2; i <= 100000; i++) { if (ffac[i].size()) continue; for (int j = i; j <= 100000; j += i) { ffac[j].push_back(i); } } for (int i = 2; i <= 100000; i++) { if (del[i]) continue; if (i < 1000 && fac[i].size() == 0) { int d = i * i; for (int j = d; j <= 100000; j += d) del[j] = true; } for (int j = i; j <= 100000; j += i) { fac[j].push_back(i); } } } void get_data() { scanf("%d%d", &N, &M); } long long fpow(long long x, int n) { long long r = 1; while (n) { if (n & 1) { r *= x; r %= MOD; } n >>= 1; x *= x; x %= MOD; } return r; } long long cal(int x, int y) { x--; y--; if (x < y) return 0; return jc[x] * fpow(jc[y], MOD - 2) % MOD * fpow(jc[x - y], MOD - 2) % MOD; } void run() { long long r = cal(N, M); for (int i = 0; i < fac[N].size(); i++) { if (ffac[fac[N][i]].size() & 1) r -= cal(N / fac[N][i], M); else r += cal(N / fac[N][i], M); r %= MOD; r += MOD; r %= MOD; } cout << r << endl; } int main() { pre(); int t; t = 1; cin >> t; while (t--) { get_data(); run(); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; template <class T> T gcd(T x, T y) { while (T t = x % y) x = y, y = t; return y; } const double eps = 1e-9; const double PI = acos(-1.); const int INF = 1000000000; const int MOD = 1000000007; const double E = 2.7182818284590452353602874713527; bool isdig(char x) { return x >= '0' && x <= '9'; } bool isup(char x) { return x >= 'A' && x <= 'Z'; } bool isdown(char x) { return x >= 'a' && x <= 'z'; } bool islet(char x) { return isup(x) || isdown(x); } int N, M; vector<int> fac[100005]; vector<int> ffac[100005]; bool del[100005]; long long jc[100005]; void pre() { jc[0] = jc[1] = 1; for (int i = 2; i <= 100000; i++) { jc[i] = jc[i - 1] * i % MOD; } for (int i = 2; i <= 100000; i++) { if (ffac[i].size()) continue; for (int j = i; j <= 100000; j += i) { ffac[j].push_back(i); } } for (int i = 2; i <= 100000; i++) { if (del[i]) continue; if (i < 1000 && fac[i].size() == 0) { int d = i * i; for (int j = d; j <= 100000; j += d) del[j] = true; } for (int j = i; j <= 100000; j += i) { fac[j].push_back(i); } } } void get_data() { scanf("%d%d", &N, &M); } long long fpow(long long x, int n) { long long r = 1; while (n) { if (n & 1) { r *= x; r %= MOD; } n >>= 1; x *= x; x %= MOD; } return r; } long long cal(int x, int y) { x--; y--; if (x < y) return 0; return jc[x] * fpow(jc[y], MOD - 2) % MOD * fpow(jc[x - y], MOD - 2) % MOD; } void run() { long long r = cal(N, M); for (int i = 0; i < fac[N].size(); i++) { if (ffac[fac[N][i]].size() & 1) r -= cal(N / fac[N][i], M); else r += cal(N / fac[N][i], M); r %= MOD; r += MOD; r %= MOD; } cout << r << endl; } int main() { pre(); int t; t = 1; cin >> t; while (t--) { get_data(); run(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int kMaxN = 1e5 + 7; constexpr ll mod = 1e9 + 7; int lp[kMaxN]; vector<int> primes; ll ans[kMaxN]; ll f[2 * kMaxN]; ll fi[2 * kMaxN]; ll powmod(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = res * a % mod; } a = a * a % mod; b /= 2; } return res; } ll C(ll n, ll k) { if (k > n) { return 0; } assert(n < kMaxN); return f[n] * fi[k] % mod * fi[n - k] % mod; } ll num_ways(ll n, ll k) { if (k > n) { return 0; } return C(n - 1, k - 1); } ll calc(int n, int f) { vector<int> factors; int tmp = n; while (tmp > 1) { if (factors.empty() || factors.back() != lp[tmp]) { factors.emplace_back(lp[tmp]); } tmp /= lp[tmp]; } ll ans = num_ways(n, f); for (int i = 1; i < (1 << factors.size()); ++i) { ll prod = 1; int cx = 0; for (int j = 0; j < factors.size(); ++j) { if (i & (1 << j)) { prod *= factors[j]; ++cx; } } if (cx & 1) { ans = (ans - num_ways(n / prod, f) + mod) % mod; } else { ans = (ans + num_ways(n / prod, f)) % mod; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); f[0] = fi[0] = 1; for (int i = 1; i < 2 * kMaxN; ++i) { f[i] = f[i - 1] * i % mod; fi[i] = powmod(f[i], mod - 2); } for (int i = 2; i < kMaxN; ++i) { if (!lp[i]) { lp[i] = i; primes.emplace_back(i); } for (int j = 0; j < primes.size() && 1LL * i * primes[j] < kMaxN && primes[j] <= lp[i]; ++j) { lp[i * primes[j]] = primes[j]; } } int q; cin >> q; while (q--) { int n, f; cin >> n >> f; cout << calc(n, f) << '\n'; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int kMaxN = 1e5 + 7; constexpr ll mod = 1e9 + 7; int lp[kMaxN]; vector<int> primes; ll ans[kMaxN]; ll f[2 * kMaxN]; ll fi[2 * kMaxN]; ll powmod(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = res * a % mod; } a = a * a % mod; b /= 2; } return res; } ll C(ll n, ll k) { if (k > n) { return 0; } assert(n < kMaxN); return f[n] * fi[k] % mod * fi[n - k] % mod; } ll num_ways(ll n, ll k) { if (k > n) { return 0; } return C(n - 1, k - 1); } ll calc(int n, int f) { vector<int> factors; int tmp = n; while (tmp > 1) { if (factors.empty() || factors.back() != lp[tmp]) { factors.emplace_back(lp[tmp]); } tmp /= lp[tmp]; } ll ans = num_ways(n, f); for (int i = 1; i < (1 << factors.size()); ++i) { ll prod = 1; int cx = 0; for (int j = 0; j < factors.size(); ++j) { if (i & (1 << j)) { prod *= factors[j]; ++cx; } } if (cx & 1) { ans = (ans - num_ways(n / prod, f) + mod) % mod; } else { ans = (ans + num_ways(n / prod, f)) % mod; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); f[0] = fi[0] = 1; for (int i = 1; i < 2 * kMaxN; ++i) { f[i] = f[i - 1] * i % mod; fi[i] = powmod(f[i], mod - 2); } for (int i = 2; i < kMaxN; ++i) { if (!lp[i]) { lp[i] = i; primes.emplace_back(i); } for (int j = 0; j < primes.size() && 1LL * i * primes[j] < kMaxN && primes[j] <= lp[i]; ++j) { lp[i * primes[j]] = primes[j]; } } int q; cin >> q; while (q--) { int n, f; cin >> n >> f; cout << calc(n, f) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_SIZE = 100500; const int mod = (int)1e9 + 7; int t, n, k; long long fact[MAX_SIZE]; long long inv[MAX_SIZE]; long long m[MAX_SIZE]; long long binpow(long long a, int b) { if (!b) { return 1; } else if (b & 1) { return (binpow(a, b - 1) * 1ll * a) % mod; } else { long long tmp = binpow(a, b >> 1); return (tmp * 1ll * tmp) % mod; } } long long f(int x) { if (n / x < k) { return 0; } else { return (((fact[n / x - 1] * 1ll * inv[n / x - k]) % mod) * inv[k - 1]) % mod; } } long long M(int x) { int mx = 0; int cnt = 0; for (int i = 2; i * 1ll * i <= x; ++i) { if (x % i == 0) { int tmp = 0; while (x % i == 0) { x /= i; ++tmp; mx = max(tmp, mx); } ++cnt; } } if (x > 1) { ++cnt; } return (mx > 1 ? 0 : ((cnt & 1) ? -1 : 1)); } long long calc() { long long ans = 0; for (int i = 1; i * 1ll * i <= n; ++i) { if (n % i == 0) { ans = (ans + (f(i) * 1ll * m[i]) % mod) % mod; if (n / i != i) { ans = (ans + (f(n / i) * 1ll * m[n / i]) % mod) % mod; } while (ans < 0) { ans += mod; } } } return ans; } inline void solve() { fact[0] = 1; inv[0] = binpow(fact[0], mod - 2); for (int i = 1; i < MAX_SIZE; ++i) { fact[i] = (fact[i - 1] * 1ll * i) % mod; inv[i] = binpow(fact[i], mod - 2); assert(fact[i] >= 0); assert(inv[i] >= 0); } for (int i = 1; i < MAX_SIZE; ++i) { m[i] = M(i); } cin >> t; while (t--) { cin >> n >> k; cout << calc() << endl; } } int main() { solve(); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_SIZE = 100500; const int mod = (int)1e9 + 7; int t, n, k; long long fact[MAX_SIZE]; long long inv[MAX_SIZE]; long long m[MAX_SIZE]; long long binpow(long long a, int b) { if (!b) { return 1; } else if (b & 1) { return (binpow(a, b - 1) * 1ll * a) % mod; } else { long long tmp = binpow(a, b >> 1); return (tmp * 1ll * tmp) % mod; } } long long f(int x) { if (n / x < k) { return 0; } else { return (((fact[n / x - 1] * 1ll * inv[n / x - k]) % mod) * inv[k - 1]) % mod; } } long long M(int x) { int mx = 0; int cnt = 0; for (int i = 2; i * 1ll * i <= x; ++i) { if (x % i == 0) { int tmp = 0; while (x % i == 0) { x /= i; ++tmp; mx = max(tmp, mx); } ++cnt; } } if (x > 1) { ++cnt; } return (mx > 1 ? 0 : ((cnt & 1) ? -1 : 1)); } long long calc() { long long ans = 0; for (int i = 1; i * 1ll * i <= n; ++i) { if (n % i == 0) { ans = (ans + (f(i) * 1ll * m[i]) % mod) % mod; if (n / i != i) { ans = (ans + (f(n / i) * 1ll * m[n / i]) % mod) % mod; } while (ans < 0) { ans += mod; } } } return ans; } inline void solve() { fact[0] = 1; inv[0] = binpow(fact[0], mod - 2); for (int i = 1; i < MAX_SIZE; ++i) { fact[i] = (fact[i - 1] * 1ll * i) % mod; inv[i] = binpow(fact[i], mod - 2); assert(fact[i] >= 0); assert(inv[i] >= 0); } for (int i = 1; i < MAX_SIZE; ++i) { m[i] = M(i); } cin >> t; while (t--) { cin >> n >> k; cout << calc() << endl; } } int main() { solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long inf = 2e18 + 7; const long long mod = 1e9 + 7; const double eps = 1e-7; const double PI = 2 * acos(0.0); const double E = 2.71828; long long fact[100005], ifact[100005]; int cnt[100005]; long long inv(long long x) { long long n = mod - 2; long long res = 1; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } long long c(int n, int k) { long long res = fact[n]; res = (res * ifact[k]) % mod; res = (res * ifact[n - k]) % mod; return res; } int n, f; bool sq[100005]; int doit(int x) { if (x < f) return 0; return c(x - 1, f - 1); } void Solve() { scanf("%d %d", &n, &f); vector<int> divs; int res = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { divs.push_back(i); if (n / i != i) divs.push_back(n / i); } } for (int(i) = 0; (i) < (divs.size()); ++(i)) { int cur = divs[i]; if (sq[cur]) continue; int add = doit(n / cur); if (cnt[cur] & 1) add = mod - add; res = (res + add) % mod; } printf("%d\n", res); } int main(void) { fact[0] = 1; for (int i = 1; i < 100005; ++i) fact[i] = (fact[i - 1] * i) % mod; for (int i = 2; i < 100005; ++i) { if (!cnt[i]) { for (int j = i; j < 100005; j += i) { ++cnt[j]; } } } for (int i = 2; i <= 2005; ++i) for (int j = i * i; j < 100005; j += i * i) { sq[j] = true; } for (int(i) = 0; (i) < (100005); ++(i)) ifact[i] = inv(fact[i]); int t; scanf("%d", &t); for (int(i) = 0; (i) < (t); ++(i)) Solve(); return 0; }
### Prompt Create a solution in Cpp for the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 2e18 + 7; const long long mod = 1e9 + 7; const double eps = 1e-7; const double PI = 2 * acos(0.0); const double E = 2.71828; long long fact[100005], ifact[100005]; int cnt[100005]; long long inv(long long x) { long long n = mod - 2; long long res = 1; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } long long c(int n, int k) { long long res = fact[n]; res = (res * ifact[k]) % mod; res = (res * ifact[n - k]) % mod; return res; } int n, f; bool sq[100005]; int doit(int x) { if (x < f) return 0; return c(x - 1, f - 1); } void Solve() { scanf("%d %d", &n, &f); vector<int> divs; int res = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { divs.push_back(i); if (n / i != i) divs.push_back(n / i); } } for (int(i) = 0; (i) < (divs.size()); ++(i)) { int cur = divs[i]; if (sq[cur]) continue; int add = doit(n / cur); if (cnt[cur] & 1) add = mod - add; res = (res + add) % mod; } printf("%d\n", res); } int main(void) { fact[0] = 1; for (int i = 1; i < 100005; ++i) fact[i] = (fact[i - 1] * i) % mod; for (int i = 2; i < 100005; ++i) { if (!cnt[i]) { for (int j = i; j < 100005; j += i) { ++cnt[j]; } } } for (int i = 2; i <= 2005; ++i) for (int j = i * i; j < 100005; j += i * i) { sq[j] = true; } for (int(i) = 0; (i) < (100005); ++(i)) ifact[i] = inv(fact[i]); int t; scanf("%d", &t); for (int(i) = 0; (i) < (t); ++(i)) Solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int mob[maxn]; vector<int> v1[maxn]; bool isPrime[maxn]; bool sq[maxn]; int fact[maxn], rfact[maxn]; int pw(int a, int b) { if (b == 0) return 1; int temp = pw(a, b / 2) % 1000000007; if (b % 2) { return (1ll * (1ll * a * temp) % 1000000007 * temp) % 1000000007; } return (1ll * temp * temp) % 1000000007; } void init() { memset(isPrime, 1, sizeof(isPrime)); isPrime[1] = 0; isPrime[0] = 0; for (int i = 1; i < maxn; i++) { for (int j = i; j < maxn; j += i) { v1[j].push_back(i); if (j != i and i != 1) { isPrime[j] = 0; } } } for (int i = 2; i * i < maxn; i++) { int k = i * i; for (int j = k; j < maxn; j += k) { sq[j] = 1; } } mob[1] = 1; for (int i = 2; i < maxn; i++) { if (sq[i]) { mob[i] = 0; continue; } int parity = 0; for (auto val : v1[i]) { if (isPrime[val]) parity++; } mob[i] = (parity % 2 == 1) ? -1 : 1; } fact[0] = 1; fact[1] = 1; rfact[1] = 1; rfact[0] = 1; for (int i = 2; i <= maxn - 1; i++) { fact[i] = (1ll * i * fact[i - 1]) % 1000000007; rfact[i] = pw(fact[i], 1000000007 - 2); } } void solve() { int n, f; scanf("%d", &n); scanf("%d", &f); int ans = 0; for (auto val : v1[n]) { int k = val; if (k < f) continue; int z = k - f; int temp = (1ll * (1ll * fact[k - 1] * rfact[z]) % 1000000007 * rfact[f - 1]) % 1000000007; if (mob[n / val] == 1) { ans = (0ll + ans + temp) % 1000000007; } else if (mob[n / val] == -1) { ans = (0ll + ans - temp + 1000000007) % 1000000007; } } printf("%d\n", ans); } int main() { init(); int t = 1; scanf("%d", &t); while (t--) { solve(); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int mob[maxn]; vector<int> v1[maxn]; bool isPrime[maxn]; bool sq[maxn]; int fact[maxn], rfact[maxn]; int pw(int a, int b) { if (b == 0) return 1; int temp = pw(a, b / 2) % 1000000007; if (b % 2) { return (1ll * (1ll * a * temp) % 1000000007 * temp) % 1000000007; } return (1ll * temp * temp) % 1000000007; } void init() { memset(isPrime, 1, sizeof(isPrime)); isPrime[1] = 0; isPrime[0] = 0; for (int i = 1; i < maxn; i++) { for (int j = i; j < maxn; j += i) { v1[j].push_back(i); if (j != i and i != 1) { isPrime[j] = 0; } } } for (int i = 2; i * i < maxn; i++) { int k = i * i; for (int j = k; j < maxn; j += k) { sq[j] = 1; } } mob[1] = 1; for (int i = 2; i < maxn; i++) { if (sq[i]) { mob[i] = 0; continue; } int parity = 0; for (auto val : v1[i]) { if (isPrime[val]) parity++; } mob[i] = (parity % 2 == 1) ? -1 : 1; } fact[0] = 1; fact[1] = 1; rfact[1] = 1; rfact[0] = 1; for (int i = 2; i <= maxn - 1; i++) { fact[i] = (1ll * i * fact[i - 1]) % 1000000007; rfact[i] = pw(fact[i], 1000000007 - 2); } } void solve() { int n, f; scanf("%d", &n); scanf("%d", &f); int ans = 0; for (auto val : v1[n]) { int k = val; if (k < f) continue; int z = k - f; int temp = (1ll * (1ll * fact[k - 1] * rfact[z]) % 1000000007 * rfact[f - 1]) % 1000000007; if (mob[n / val] == 1) { ans = (0ll + ans + temp) % 1000000007; } else if (mob[n / val] == -1) { ans = (0ll + ans - temp + 1000000007) % 1000000007; } } printf("%d\n", ans); } int main() { init(); int t = 1; scanf("%d", &t); while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxN = 100 * 1000 + 5; const long long mod = 1000 * 1000 * 1000 + 7; int pi[maxN]; long long mul(long long x, long long y) { return (x * y) % mod; } long long add(long long x, long long y) { return (x + y + mod) % mod; } long long pw(long long x, int k) { if (k == 0) return 1; long long res = pw(x, k / 2); res = mul(res, res); if (k % 2) res = mul(res, x); return res; } long long div(long long x) { return pw(x, mod - 2); } long long fact[maxN]; long long g[maxN]; long long c(int x, int y) { return mul(fact[x], mul(g[y], g[x - y])); } vector<int> b[maxN]; map<pair<int, int>, long long> d; long long solve(int n, int k) { if (k > n) return 0; if (n == 1) return 1; pair<int, int> st = make_pair(n, k); if (d.find(st) != d.end()) return d[st]; long long sum = c(n - 1, k - 1); for (int i = 0; i < ((int)(b[n]).size()); i++) { int x = b[n][i]; long long tmp = solve(n / x, k); sum = add(sum, -tmp); } return d[st] = sum; } int main() { ios_base::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < maxN; i++) fact[i] = mul(fact[i - 1], i); for (int i = 2; i < maxN; i++) for (int j = i; j < maxN; j += i) b[j].push_back(i); for (int i = 0; i < maxN; i++) g[i] = div(fact[i]); int m; cin >> m; for (int q = 0; q < m; q++) { int n, k; cin >> n >> k; cout << solve(n, k) << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxN = 100 * 1000 + 5; const long long mod = 1000 * 1000 * 1000 + 7; int pi[maxN]; long long mul(long long x, long long y) { return (x * y) % mod; } long long add(long long x, long long y) { return (x + y + mod) % mod; } long long pw(long long x, int k) { if (k == 0) return 1; long long res = pw(x, k / 2); res = mul(res, res); if (k % 2) res = mul(res, x); return res; } long long div(long long x) { return pw(x, mod - 2); } long long fact[maxN]; long long g[maxN]; long long c(int x, int y) { return mul(fact[x], mul(g[y], g[x - y])); } vector<int> b[maxN]; map<pair<int, int>, long long> d; long long solve(int n, int k) { if (k > n) return 0; if (n == 1) return 1; pair<int, int> st = make_pair(n, k); if (d.find(st) != d.end()) return d[st]; long long sum = c(n - 1, k - 1); for (int i = 0; i < ((int)(b[n]).size()); i++) { int x = b[n][i]; long long tmp = solve(n / x, k); sum = add(sum, -tmp); } return d[st] = sum; } int main() { ios_base::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < maxN; i++) fact[i] = mul(fact[i - 1], i); for (int i = 2; i < maxN; i++) for (int j = i; j < maxN; j += i) b[j].push_back(i); for (int i = 0; i < maxN; i++) g[i] = div(fact[i]); int m; cin >> m; for (int q = 0; q < m; q++) { int n, k; cin >> n >> k; cout << solve(n, k) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100]; int q, n, f, prime[100100], sqr[100100], mu[100100], ans; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; else if (ans >= MOD) ans -= MOD; } printf("%d\n", ans); } return 0; }
### Prompt Generate a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100]; int q, n, f, prime[100100], sqr[100100], mu[100100], ans; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; else if (ans >= MOD) ans -= MOD; } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int MAX = 1e5 + 5; const int P = (1 << 6) + 5; const int MOD = 1e9 + 7; const int64_t INF = INT_MAX; const int64_t _INF = INT_MIN; int sieve[MAX], fac[MAX], infac[MAX]; int prod[P]; bool odd[P]; int64_t add(int64_t a, int64_t b) { return (a + b) % MOD; } int64_t sub(int64_t a, int64_t b) { return (a - b + MOD) % MOD; } int64_t mul(int64_t a, int64_t b) { return (a * b) % MOD; } int inv(int val) { int curr = 1, temp = MOD - 2; while (temp > 0) { if (temp & 1) curr = mul(curr, val); val = mul(val, val); temp >>= 1; } return curr; } void precompute() { for (int64_t i = 2; i < MAX; i++) { if (!sieve[i]) { sieve[i] = i; for (int64_t j = i * i; j < MAX; j += i) { if (!sieve[j]) sieve[j] = i; } } } fac[0] = 1; for (int i = 1; i <= MAX - 1; i++) fac[i] = mul(i, fac[i - 1]); infac[MAX - 1] = inv(fac[MAX - 1]); for (int i = MAX - 2; i >= 0; i--) infac[i] = mul(i + 1, infac[i + 1]); prod[0] = 1; for (int i = 1; i <= P - 1; i++) odd[i] = odd[i ^ (i & -i)] ^ 1; } int nCr(int a, int b) { return (b > a) ? 0 : mul(mul(fac[a], infac[a - b]), infac[b]); } int solve(int n, int f) { if (n == 1) return 1; int curr = 0, cnt = 0, val = n; while (val > 1) { if (sieve[val] != curr) { curr = sieve[val]; prod[1 << (cnt++)] = curr; } val /= curr; } int total = nCr(n - 1, f - 1), invalid = 0, m = (1 << cnt) - 1; for (int i = 1; i <= m; i++) { prod[i] = prod[i & -i] * prod[i ^ (i & -i)]; if (odd[i]) invalid = add(invalid, nCr(n / prod[i] - 1, f - 1)); else invalid = sub(invalid, nCr(n / prod[i] - 1, f - 1)); } return sub(total, invalid); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); precompute(); int q; cin >> q; while (q--) { int n, f; cin >> n >> f; cout << solve(n, f) << "\n"; } }
### Prompt Generate a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int MAX = 1e5 + 5; const int P = (1 << 6) + 5; const int MOD = 1e9 + 7; const int64_t INF = INT_MAX; const int64_t _INF = INT_MIN; int sieve[MAX], fac[MAX], infac[MAX]; int prod[P]; bool odd[P]; int64_t add(int64_t a, int64_t b) { return (a + b) % MOD; } int64_t sub(int64_t a, int64_t b) { return (a - b + MOD) % MOD; } int64_t mul(int64_t a, int64_t b) { return (a * b) % MOD; } int inv(int val) { int curr = 1, temp = MOD - 2; while (temp > 0) { if (temp & 1) curr = mul(curr, val); val = mul(val, val); temp >>= 1; } return curr; } void precompute() { for (int64_t i = 2; i < MAX; i++) { if (!sieve[i]) { sieve[i] = i; for (int64_t j = i * i; j < MAX; j += i) { if (!sieve[j]) sieve[j] = i; } } } fac[0] = 1; for (int i = 1; i <= MAX - 1; i++) fac[i] = mul(i, fac[i - 1]); infac[MAX - 1] = inv(fac[MAX - 1]); for (int i = MAX - 2; i >= 0; i--) infac[i] = mul(i + 1, infac[i + 1]); prod[0] = 1; for (int i = 1; i <= P - 1; i++) odd[i] = odd[i ^ (i & -i)] ^ 1; } int nCr(int a, int b) { return (b > a) ? 0 : mul(mul(fac[a], infac[a - b]), infac[b]); } int solve(int n, int f) { if (n == 1) return 1; int curr = 0, cnt = 0, val = n; while (val > 1) { if (sieve[val] != curr) { curr = sieve[val]; prod[1 << (cnt++)] = curr; } val /= curr; } int total = nCr(n - 1, f - 1), invalid = 0, m = (1 << cnt) - 1; for (int i = 1; i <= m; i++) { prod[i] = prod[i & -i] * prod[i ^ (i & -i)]; if (odd[i]) invalid = add(invalid, nCr(n / prod[i] - 1, f - 1)); else invalid = sub(invalid, nCr(n / prod[i] - 1, f - 1)); } return sub(total, invalid); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); precompute(); int q; cin >> q; while (q--) { int n, f; cin >> n >> f; cout << solve(n, f) << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } long long int converter(string a) { long long int i, mul = 1, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } const int MAX = 100005; long long int n, inv[MAX]; long long int fact[MAX]; int mu[MAX]; void mobius() { int i, j; mu[1] = fact[0] = fact[1] = 1; for (i = 1; i < MAX; i++) { for (j = i + i; j < MAX; j += i) { mu[j] -= mu[i]; } fact[i] = (fact[i - 1] * i) % MOD; } } long long int stars_bars(long long int nn, long long int kk) { long long int cur = (inv[kk - 1] * inv[nn - kk]) % MOD; cur = (fact[nn - 1] * cur) % MOD; return cur; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int i, f, qu, div1, div2; mobius(); for (i = 0; i < MAX; ++i) { inv[i] = modDiv(1LL, fact[i]); } cin >> qu; while (qu--) { cin >> n >> f; int lim = sqrt(n); long long int tot = 0LL; for (i = 1; i <= (lim); ++i) { if (n % i != 0) continue; div1 = n / i; if (div1 >= f) { tot += (mu[i] * stars_bars(div1, f)); if (tot >= MOD) tot -= MOD; else if (tot < 0) tot += MOD; } if (div1 * div1 != n) { div2 = n / div1; if (div2 >= f) { tot += (mu[div1] * stars_bars(div2, f)); if (tot >= MOD) tot -= MOD; else if (tot < 0) tot += MOD; } } } cout << tot << '\n'; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } long long int converter(string a) { long long int i, mul = 1, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } const int MAX = 100005; long long int n, inv[MAX]; long long int fact[MAX]; int mu[MAX]; void mobius() { int i, j; mu[1] = fact[0] = fact[1] = 1; for (i = 1; i < MAX; i++) { for (j = i + i; j < MAX; j += i) { mu[j] -= mu[i]; } fact[i] = (fact[i - 1] * i) % MOD; } } long long int stars_bars(long long int nn, long long int kk) { long long int cur = (inv[kk - 1] * inv[nn - kk]) % MOD; cur = (fact[nn - 1] * cur) % MOD; return cur; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int i, f, qu, div1, div2; mobius(); for (i = 0; i < MAX; ++i) { inv[i] = modDiv(1LL, fact[i]); } cin >> qu; while (qu--) { cin >> n >> f; int lim = sqrt(n); long long int tot = 0LL; for (i = 1; i <= (lim); ++i) { if (n % i != 0) continue; div1 = n / i; if (div1 >= f) { tot += (mu[i] * stars_bars(div1, f)); if (tot >= MOD) tot -= MOD; else if (tot < 0) tot += MOD; } if (div1 * div1 != n) { div2 = n / div1; if (div2 >= f) { tot += (mu[div1] * stars_bars(div2, f)); if (tot >= MOD) tot -= MOD; else if (tot < 0) tot += MOD; } } } cout << tot << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7, maxn = 1e5; int n, f; long long P[maxn], D[maxn]; vector<int> facs; map<pair<int, int>, int> ans; int Pow(int x, int k) { if (k == 0) return 1; long long tmp = Pow(x, k >> 1); tmp = (tmp * tmp) % M; if (k & 1) return (tmp * x) % M; else return tmp; } int Sum(int n, int f) { if (ans.find(make_pair(n, f)) != ans.end()) return ans[make_pair(n, f)]; if (n < f) return 0; if (f == 1) { if (n == 1) return 1; else return 0; } long long tmp = ((P[n - 1] * D[f - 1]) % M * D[n - f]) % M; int p; for (p = 2; p * p < n; p++) if (n % p == 0) tmp = (tmp - Sum(p, f) - Sum(n / p, f)) % M; if (p * p == n) tmp -= Sum(p, f); tmp %= M; ans[make_pair(n, f)] = tmp; return tmp; } int main() { D[0] = P[0] = 1LL; for (int i = 1; i < maxn; i++) { P[i] = (P[i - 1] * i) % M; D[i] = Pow(P[i], M - 2); } int Q; scanf("%d", &Q); while (Q--) { scanf("%d%d", &n, &f); printf("%d\n", (Sum(n, f) + M) % M); } }
### Prompt Please formulate a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7, maxn = 1e5; int n, f; long long P[maxn], D[maxn]; vector<int> facs; map<pair<int, int>, int> ans; int Pow(int x, int k) { if (k == 0) return 1; long long tmp = Pow(x, k >> 1); tmp = (tmp * tmp) % M; if (k & 1) return (tmp * x) % M; else return tmp; } int Sum(int n, int f) { if (ans.find(make_pair(n, f)) != ans.end()) return ans[make_pair(n, f)]; if (n < f) return 0; if (f == 1) { if (n == 1) return 1; else return 0; } long long tmp = ((P[n - 1] * D[f - 1]) % M * D[n - f]) % M; int p; for (p = 2; p * p < n; p++) if (n % p == 0) tmp = (tmp - Sum(p, f) - Sum(n / p, f)) % M; if (p * p == n) tmp -= Sum(p, f); tmp %= M; ans[make_pair(n, f)] = tmp; return tmp; } int main() { D[0] = P[0] = 1LL; for (int i = 1; i < maxn; i++) { P[i] = (P[i - 1] * i) % M; D[i] = Pow(P[i], M - 2); } int Q; scanf("%d", &Q); while (Q--) { scanf("%d%d", &n, &f); printf("%d\n", (Sum(n, f) + M) % M); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 10; const int MOD = 1000 * 1000 * 1000 + 7; vector<int> dv[MAXN]; int f; long long int dp[MAXN]; int mark[MAXN]; int zz; long long int po(long long int p, long long int b) { if (!p) return 1; long long int r = po(p / 2, b); r = (r * r) % MOD; if (p & 1) r = (r * b) % MOD; return r; } long long int fac[MAXN]; long long int rfac[MAXN]; long long int comb(long long int n, long long int k) { long long int res = fac[n]; res *= rfac[k]; res %= MOD; res *= rfac[n - k]; res %= MOD; return res; } long long int cdp(int x) { long long int& ans = dp[x]; if (f - 1 > x - 1) return 0; if (mark[x] == zz) return ans; mark[x] = zz; long long int kol = comb(x - 1, f - 1); long long int bad = 0; for (int i = 0; i < dv[x].size(); i++) { bad += cdp(x / dv[x][i]); bad %= MOD; } ans = (kol + MOD - bad) % MOD; return ans; } int main() { ios::sync_with_stdio(false); fac[0] = fac[1] = 1; for (int i = 2; i < MAXN; i++) { fac[i] = (fac[i - 1] * i) % MOD; } for (int i = 0; i < MAXN; i++) rfac[i] = po(MOD - 2, fac[i]); for (int i = 2; i < MAXN; i++) for (int j = i; j < MAXN; j += i) dv[j].push_back(i); int tt; cin >> tt; for (zz = 1; zz <= tt; zz++) { int n; cin >> n >> f; cout << cdp(n) << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 10; const int MOD = 1000 * 1000 * 1000 + 7; vector<int> dv[MAXN]; int f; long long int dp[MAXN]; int mark[MAXN]; int zz; long long int po(long long int p, long long int b) { if (!p) return 1; long long int r = po(p / 2, b); r = (r * r) % MOD; if (p & 1) r = (r * b) % MOD; return r; } long long int fac[MAXN]; long long int rfac[MAXN]; long long int comb(long long int n, long long int k) { long long int res = fac[n]; res *= rfac[k]; res %= MOD; res *= rfac[n - k]; res %= MOD; return res; } long long int cdp(int x) { long long int& ans = dp[x]; if (f - 1 > x - 1) return 0; if (mark[x] == zz) return ans; mark[x] = zz; long long int kol = comb(x - 1, f - 1); long long int bad = 0; for (int i = 0; i < dv[x].size(); i++) { bad += cdp(x / dv[x][i]); bad %= MOD; } ans = (kol + MOD - bad) % MOD; return ans; } int main() { ios::sync_with_stdio(false); fac[0] = fac[1] = 1; for (int i = 2; i < MAXN; i++) { fac[i] = (fac[i - 1] * i) % MOD; } for (int i = 0; i < MAXN; i++) rfac[i] = po(MOD - 2, fac[i]); for (int i = 2; i < MAXN; i++) for (int j = i; j < MAXN; j += i) dv[j].push_back(i); int tt; cin >> tt; for (zz = 1; zz <= tt; zz++) { int n; cin >> n >> f; cout << cdp(n) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100], ans; int q, n, f, prime[100100], sqr[100100], mu[100100]; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; int t = cnt & 1; mu[i] = t ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; } printf("%d\n", (int)ans); } return 0; }
### Prompt Generate a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100], ans; int q, n, f, prime[100100], sqr[100100], mu[100100]; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; int t = cnt & 1; mu[i] = t ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; } printf("%d\n", (int)ans); } return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; long long fact[100005]; int q, used[100005], dp[100005], deg[100005]; vector<int> divisors[100005]; int power(long long a, long long b) { if (!b) return 1; long long t = power(a, b / 2); if (b % 2) return (((t * t) % 1000000007) * a) % 1000000007; else return (t * t) % 1000000007; } int C(int x, int y) { if (y < 0 || y > x) return 0; if (!y) return 1; long long a = (fact[y] * fact[x - y]) % 1000000007; return ((fact[x] * (long long)deg[y]) % 1000000007 * (long long)deg[x - y]) % 1000000007; } int memo(int x, int y) { if (y > x) return 0; if (x == y) return 1; if (used[x] == q) return dp[x]; used[x] = q; int res = C(x - 1, y - 1); for (int i = 0; i < divisors[x].size(); i++) { res -= memo(x / divisors[x][i], y); if (res < 0) res += 1000000007; res %= 1000000007; } dp[x] = res; return res; } int main() { scanf("%d", &q); for (int i = 2; i <= 100000; i++) { int x = i; while (x <= 100000) { divisors[x].push_back(i); x += i; } } fact[0] = 1; for (int i = 1; i <= 100000; i++) fact[i] = (fact[i - 1] * (long long)i) % 1000000007, deg[i] = power(fact[i], 1000000007 - 2); while (q) { int x, y; scanf("%d%d", &x, &y); if (y == 1 && x > 1) { puts("0"); q--; continue; } printf("%d\n", memo(x, y)); q--; } }
### Prompt Please provide a CPP coded solution to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; long long fact[100005]; int q, used[100005], dp[100005], deg[100005]; vector<int> divisors[100005]; int power(long long a, long long b) { if (!b) return 1; long long t = power(a, b / 2); if (b % 2) return (((t * t) % 1000000007) * a) % 1000000007; else return (t * t) % 1000000007; } int C(int x, int y) { if (y < 0 || y > x) return 0; if (!y) return 1; long long a = (fact[y] * fact[x - y]) % 1000000007; return ((fact[x] * (long long)deg[y]) % 1000000007 * (long long)deg[x - y]) % 1000000007; } int memo(int x, int y) { if (y > x) return 0; if (x == y) return 1; if (used[x] == q) return dp[x]; used[x] = q; int res = C(x - 1, y - 1); for (int i = 0; i < divisors[x].size(); i++) { res -= memo(x / divisors[x][i], y); if (res < 0) res += 1000000007; res %= 1000000007; } dp[x] = res; return res; } int main() { scanf("%d", &q); for (int i = 2; i <= 100000; i++) { int x = i; while (x <= 100000) { divisors[x].push_back(i); x += i; } } fact[0] = 1; for (int i = 1; i <= 100000; i++) fact[i] = (fact[i - 1] * (long long)i) % 1000000007, deg[i] = power(fact[i], 1000000007 - 2); while (q) { int x, y; scanf("%d%d", &x, &y); if (y == 1 && x > 1) { puts("0"); q--; continue; } printf("%d\n", memo(x, y)); q--; } } ```
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& a) { return os << "(" << a.first << "," << a.second << ")"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << (*i) << " "; cout << endl; } template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } int nextInt() { int x; scanf("%d", &x); return x; } const long long MOD = 1000000007; const int SZ = 312345; long long inv[SZ]; long long fact[SZ]; long long fact_inv[SZ]; long long choose(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; return fact[n] * fact_inv[n - r] % MOD * fact_inv[r] % MOD; } vector<int> primes; bool isPrime[SZ]; void make_primes() { if (primes.size() > 0) return; memset(isPrime, true, sizeof(isPrime)); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < SZ; i++) if (isPrime[i]) for (int j = i * i; j < SZ; j += i) isPrime[j] = false; for (int i = 0, _n = (int)(SZ); i < _n; ++i) if (isPrime[i]) primes.push_back(i); } const int MAX_N = SZ; int mobius[MAX_N]; void make_mobius() { for (int i = 0; i < MAX_N; i++) mobius[i] = 1; for (int i = 2; i < MAX_N; i++) if (isPrime[i]) for (int j = i; j < MAX_N; j += i) if (j / i % i == 0) mobius[j] = 0; else mobius[j] *= -1; } long long H(int n, int m) { return choose(n + m - 1, n); } long long p(int n, int f) { if (n < f) return 0; return H(n - f, f); } int main2() { int q = nextInt(); for (; q--;) { int n = nextInt(); int f = nextInt(); long long ans = 0; for (int d = 1; d * d <= n; d++) { if (n % d == 0) { int x = d; if (mobius[x]) { ans += mobius[x] * p(n / x, f); ans %= MOD; } if (d * d != n) { int x = n / d; if (mobius[x]) { ans += mobius[x] * p(n / x, f); ans %= MOD; } } } } ans = ((ans % MOD) + MOD) % MOD; printf("%lld\n", ans); } return 0; } int main() { make_primes(); make_mobius(); inv[1] = 1; for (int i = 2; i < SZ; i++) inv[i] = inv[(int)(MOD % i)] * (MOD - MOD / i) % MOD; fact[0] = 1; for (int i = 1; i < SZ; i++) fact[i] = fact[i - 1] * i % MOD; fact_inv[0] = 1; for (int i = 1; i < SZ; i++) fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; for (; !cin.eof(); cin >> ws) main2(); return 0; }
### Prompt Create a solution in CPP for the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& a) { return os << "(" << a.first << "," << a.second << ")"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << (*i) << " "; cout << endl; } template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } int nextInt() { int x; scanf("%d", &x); return x; } const long long MOD = 1000000007; const int SZ = 312345; long long inv[SZ]; long long fact[SZ]; long long fact_inv[SZ]; long long choose(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; return fact[n] * fact_inv[n - r] % MOD * fact_inv[r] % MOD; } vector<int> primes; bool isPrime[SZ]; void make_primes() { if (primes.size() > 0) return; memset(isPrime, true, sizeof(isPrime)); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < SZ; i++) if (isPrime[i]) for (int j = i * i; j < SZ; j += i) isPrime[j] = false; for (int i = 0, _n = (int)(SZ); i < _n; ++i) if (isPrime[i]) primes.push_back(i); } const int MAX_N = SZ; int mobius[MAX_N]; void make_mobius() { for (int i = 0; i < MAX_N; i++) mobius[i] = 1; for (int i = 2; i < MAX_N; i++) if (isPrime[i]) for (int j = i; j < MAX_N; j += i) if (j / i % i == 0) mobius[j] = 0; else mobius[j] *= -1; } long long H(int n, int m) { return choose(n + m - 1, n); } long long p(int n, int f) { if (n < f) return 0; return H(n - f, f); } int main2() { int q = nextInt(); for (; q--;) { int n = nextInt(); int f = nextInt(); long long ans = 0; for (int d = 1; d * d <= n; d++) { if (n % d == 0) { int x = d; if (mobius[x]) { ans += mobius[x] * p(n / x, f); ans %= MOD; } if (d * d != n) { int x = n / d; if (mobius[x]) { ans += mobius[x] * p(n / x, f); ans %= MOD; } } } } ans = ((ans % MOD) + MOD) % MOD; printf("%lld\n", ans); } return 0; } int main() { make_primes(); make_mobius(); inv[1] = 1; for (int i = 2; i < SZ; i++) inv[i] = inv[(int)(MOD % i)] * (MOD - MOD / i) % MOD; fact[0] = 1; for (int i = 1; i < SZ; i++) fact[i] = fact[i - 1] * i % MOD; fact_inv[0] = 1; for (int i = 1; i < SZ; i++) fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; for (; !cin.eof(); cin >> ws) main2(); return 0; } ```
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, int> dp; int f[200001], t, n, m, d[20]; int binpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = (res * 1ll * a) % 1000000007; b >>= 1; a = (a * 1ll * a) % 1000000007; } return res; } int get_c(int n, int k) { return f[n] * 1ll * binpow(f[n - k] * 1ll * f[k] % 1000000007, 1000000007 - 2) % 1000000007; } int solve(int n, int f) { pair<int, int> state = make_pair(n, f); if (dp.count(state)) return dp[state]; if (n == 1) return dp[state] = (f == 1 ? 1 : 0); if (n < f) return dp[state] = 0; int sz = 0, x = n; for (int i = 2; i * i <= x; ++i) if (x % i == 0) { d[sz++] = i; while (x % i == 0) x /= i; } if (x > 1) d[sz++] = x; int &ans = dp[state]; for (int mask = 1; mask < (1 << sz); ++mask) { int cur = 1; for (int i = 0; i < sz; ++i) if (mask >> i & 1) cur *= d[i]; if (n / cur < f) continue; cur = get_c(n / cur - 1, f - 1); if (__builtin_popcount(mask) & 1) ans += cur; else ans -= cur; if (ans >= 1000000007) ans -= 1000000007; if (ans < 0) ans += 1000000007; } if (n == 9) cerr << ans; return ans = (get_c(n - 1, f - 1) - ans + 1000000007) % 1000000007; } int main() { scanf("%d", &t); f[0] = 1; for (int i = 1; i <= 100000; ++i) f[i] = f[i - 1] * 1ll * i % 1000000007; while (t--) { scanf("%d%d", &n, &m); printf("%d\n", solve(n, m)); } }
### Prompt In Cpp, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<pair<int, int>, int> dp; int f[200001], t, n, m, d[20]; int binpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = (res * 1ll * a) % 1000000007; b >>= 1; a = (a * 1ll * a) % 1000000007; } return res; } int get_c(int n, int k) { return f[n] * 1ll * binpow(f[n - k] * 1ll * f[k] % 1000000007, 1000000007 - 2) % 1000000007; } int solve(int n, int f) { pair<int, int> state = make_pair(n, f); if (dp.count(state)) return dp[state]; if (n == 1) return dp[state] = (f == 1 ? 1 : 0); if (n < f) return dp[state] = 0; int sz = 0, x = n; for (int i = 2; i * i <= x; ++i) if (x % i == 0) { d[sz++] = i; while (x % i == 0) x /= i; } if (x > 1) d[sz++] = x; int &ans = dp[state]; for (int mask = 1; mask < (1 << sz); ++mask) { int cur = 1; for (int i = 0; i < sz; ++i) if (mask >> i & 1) cur *= d[i]; if (n / cur < f) continue; cur = get_c(n / cur - 1, f - 1); if (__builtin_popcount(mask) & 1) ans += cur; else ans -= cur; if (ans >= 1000000007) ans -= 1000000007; if (ans < 0) ans += 1000000007; } if (n == 9) cerr << ans; return ans = (get_c(n - 1, f - 1) - ans + 1000000007) % 1000000007; } int main() { scanf("%d", &t); f[0] = 1; for (int i = 1; i <= 100000; ++i) f[i] = f[i - 1] * 1ll * i % 1000000007; while (t--) { scanf("%d%d", &n, &m); printf("%d\n", solve(n, m)); } } ```
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100], ans; int q, n, f, prime[100100], sqr[100100], mu[100100]; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; } printf("%d\n", (int)ans); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100], ans; int q, n, f, prime[100100], sqr[100100], mu[100100]; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y % 2) ret = (ret * x) % MOD; x = (x * x) % MOD; y >>= 1; } return ret; } long long C(int x, int y) { long long ret = (inv_ff[y] * inv_ff[x - y]) % MOD; return (ff[x] * ret) % MOD; } long long solve(int x, int y) { if (y > x) return 0; return C(x - 1, y - 1); } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * solve(n / (*d), f)) % MOD; if (ans < 0) ans += MOD; } printf("%d\n", (int)ans); } return 0; } ```
#include <bits/stdc++.h> const double EPS = 1e-24; const long long int MOD = 1000000007ll; const double PI = 3.14159265359; int INF = 2147483645; template <class T> T Max2(T a, T b) { return a < b ? b : a; } template <class T> T Min2(T a, T b) { return a < b ? a : b; } template <class T> T Max3(T a, T b, T c) { return Max2(Max2(a, b), c); } template <class T> T Min3(T a, T b, T c) { return Min2(Min2(a, b), c); } template <class T> T Max4(T a, T b, T c, T d) { return Max2(Max2(a, b), Max2(c, d)); } template <class T> T Min4(T a, T b, T c, T d) { return Min2(Min2(a, b), Max2(c, d)); } using namespace std; long long int q, f, n; long long int ans; long long int fact[100010]; long long int invfact[100010]; vector<long long int> factors[100010]; bool isp[100010]; long long int powermod(long long int _a, long long int _b, long long int _m) { long long int _ret = 1; while (_b) { if (_b % 2 == 1) _ret = (_ret * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _ret; } long long int solve(long long int n, long long int r) { if (n < r) return 0; long long int ret = fact[n]; ret = (ret * invfact[r]) % MOD; ret = (ret * invfact[n - r]) % MOD; return ret; } long long int dfs(long long int s, long long int v, long long int i) { if (i == factors[v].size()) { if (factors[s].size() % 2 == 0) return solve(f / s - 1, n - 1); else return -solve(f / s - 1, n - 1); } else return (dfs(s, v, i + 1) + dfs(s * factors[v][i], v, i + 1)) % MOD; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); fact[0] = 1; for (int i = 1; i <= 100001; i++) fact[i] = (fact[i - 1] * i) % MOD; invfact[0] = invfact[1] = 1; for (int i = 2; i <= 100001; i++) invfact[i] = (invfact[i - 1] * powermod(i, MOD - 2, MOD)) % MOD; for (int i = 2; i <= 100001; i++) isp[i] = true; for (int i = 2; i <= 100001; i++) { if (isp[i]) { factors[i].push_back(i); for (int j = i + i; j <= 100001; j += i) isp[j] = false, factors[j].push_back(i); } } cin >> q; while (q--) { cin >> f >> n; cout << (dfs(1, f, 0) + MOD) % MOD << "\n"; } return 0; }
### Prompt Generate a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> const double EPS = 1e-24; const long long int MOD = 1000000007ll; const double PI = 3.14159265359; int INF = 2147483645; template <class T> T Max2(T a, T b) { return a < b ? b : a; } template <class T> T Min2(T a, T b) { return a < b ? a : b; } template <class T> T Max3(T a, T b, T c) { return Max2(Max2(a, b), c); } template <class T> T Min3(T a, T b, T c) { return Min2(Min2(a, b), c); } template <class T> T Max4(T a, T b, T c, T d) { return Max2(Max2(a, b), Max2(c, d)); } template <class T> T Min4(T a, T b, T c, T d) { return Min2(Min2(a, b), Max2(c, d)); } using namespace std; long long int q, f, n; long long int ans; long long int fact[100010]; long long int invfact[100010]; vector<long long int> factors[100010]; bool isp[100010]; long long int powermod(long long int _a, long long int _b, long long int _m) { long long int _ret = 1; while (_b) { if (_b % 2 == 1) _ret = (_ret * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _ret; } long long int solve(long long int n, long long int r) { if (n < r) return 0; long long int ret = fact[n]; ret = (ret * invfact[r]) % MOD; ret = (ret * invfact[n - r]) % MOD; return ret; } long long int dfs(long long int s, long long int v, long long int i) { if (i == factors[v].size()) { if (factors[s].size() % 2 == 0) return solve(f / s - 1, n - 1); else return -solve(f / s - 1, n - 1); } else return (dfs(s, v, i + 1) + dfs(s * factors[v][i], v, i + 1)) % MOD; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); fact[0] = 1; for (int i = 1; i <= 100001; i++) fact[i] = (fact[i - 1] * i) % MOD; invfact[0] = invfact[1] = 1; for (int i = 2; i <= 100001; i++) invfact[i] = (invfact[i - 1] * powermod(i, MOD - 2, MOD)) % MOD; for (int i = 2; i <= 100001; i++) isp[i] = true; for (int i = 2; i <= 100001; i++) { if (isp[i]) { factors[i].push_back(i); for (int j = i + i; j <= 100001; j += i) isp[j] = false, factors[j].push_back(i); } } cin >> q; while (q--) { cin >> f >> n; cout << (dfs(1, f, 0) + MOD) % MOD << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long mod = 1e9 + 7; long long fact[N], inv[N]; long long dp[N]; int done[N]; int q; int id; vector<long long> g[N]; long long bigmod(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } void pre() { fact[0] = 1, inv[0] = bigmod(1, mod - 2); for (int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = bigmod(fact[i], mod - 2); for (int j = i; j < N; j += i) { g[j].push_back(i); } } } long long ncr(long long n, long long r) { if (n < r or r < 0) return 0; long long up = (fact[n] * inv[r]) % mod; up = (up * inv[n - r]) % mod; return up; } long long go(long long n, long long f) { if (n < f) return 0; if (n == f) return 1; if (done[n] == id) return dp[n]; done[n] = id; long long &ways = dp[n]; ways = ncr(n - 1, f - 1); for (auto x : g[n]) if (x != n) { ways = (ways - go(x, f)); if (ways < 0) ways += mod; } return ways; } int main() { pre(); scanf("%d", &q); while (q--) { id++; long long n, f; scanf("%lld %lld", &n, &f); printf("%lld\n", go(n, f)); } }
### Prompt Create a solution in Cpp for the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long mod = 1e9 + 7; long long fact[N], inv[N]; long long dp[N]; int done[N]; int q; int id; vector<long long> g[N]; long long bigmod(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } void pre() { fact[0] = 1, inv[0] = bigmod(1, mod - 2); for (int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = bigmod(fact[i], mod - 2); for (int j = i; j < N; j += i) { g[j].push_back(i); } } } long long ncr(long long n, long long r) { if (n < r or r < 0) return 0; long long up = (fact[n] * inv[r]) % mod; up = (up * inv[n - r]) % mod; return up; } long long go(long long n, long long f) { if (n < f) return 0; if (n == f) return 1; if (done[n] == id) return dp[n]; done[n] = id; long long &ways = dp[n]; ways = ncr(n - 1, f - 1); for (auto x : g[n]) if (x != n) { ways = (ways - go(x, f)); if (ways < 0) ways += mod; } return ways; } int main() { pre(); scanf("%d", &q); while (q--) { id++; long long n, f; scanf("%lld %lld", &n, &f); printf("%lld\n", go(n, f)); } } ```
#include <bits/stdc++.h> using namespace std; int mpow(int base, int exp); void ipgraph(int m); void dfs(int u, int par); const int mod = 1000000007; const int N = 3e5, M = N; long long fac[N], ifac[N]; vector<int> g[N]; int a[N]; long long ff(int n, int f) { n--, f--; if (n < f) return 0; long long ans = fac[n]; ans *= ifac[f]; ans %= mod; ans *= ifac[n - f]; ans %= mod; return ans; } vector<int> p, sq, mu; void pre() { int i, j; p.resize(N, 1); sq.resize(N, 0); mu.resize(N, 0); p[0] = p[1] = 0; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (p[i]) { for (j = 2 * i; j < N; j += i) p[j] = 0; } } for (i = 1; 1 < N ? i < N : i > N; 1 < N ? i += 1 : i -= 1) { for (j = i; j < N; j += i) { g[j].push_back(i); } } for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (i * 1LL * i > N) break; int x = i * i; for (j = x; j < N; j += x) { sq[j] = 1; } } mu[1] = 1; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (sq[i] == 1) continue; int tot = 0; for (int x : g[i]) tot += p[x]; mu[i] = (tot & 1) ? -1 : 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, n, m, k, j, f; pre(); fac[0] = fac[1] = 1; ifac[0] = ifac[1] = 1; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) fac[i] = (fac[i - 1] * 1LL * i) % mod; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) ifac[i] = (ifac[i - 1] * 1LL * mpow(i, mod - 2)) % mod; int q; cin >> q; while (q--) { cin >> n >> f; if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; continue; } long long ans = 0; for (int x : g[n]) { ans += (mu[n / x] * ff(x, f)) % mod; if (ans < 0) ans += mod; if (ans >= mod) ans -= mod; } cout << ans << endl; } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((long long)result * base) % mod; base = ((long long)base * base) % mod; exp >>= 1; } return result; }
### Prompt Construct a cpp code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int mpow(int base, int exp); void ipgraph(int m); void dfs(int u, int par); const int mod = 1000000007; const int N = 3e5, M = N; long long fac[N], ifac[N]; vector<int> g[N]; int a[N]; long long ff(int n, int f) { n--, f--; if (n < f) return 0; long long ans = fac[n]; ans *= ifac[f]; ans %= mod; ans *= ifac[n - f]; ans %= mod; return ans; } vector<int> p, sq, mu; void pre() { int i, j; p.resize(N, 1); sq.resize(N, 0); mu.resize(N, 0); p[0] = p[1] = 0; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (p[i]) { for (j = 2 * i; j < N; j += i) p[j] = 0; } } for (i = 1; 1 < N ? i < N : i > N; 1 < N ? i += 1 : i -= 1) { for (j = i; j < N; j += i) { g[j].push_back(i); } } for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (i * 1LL * i > N) break; int x = i * i; for (j = x; j < N; j += x) { sq[j] = 1; } } mu[1] = 1; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { if (sq[i] == 1) continue; int tot = 0; for (int x : g[i]) tot += p[x]; mu[i] = (tot & 1) ? -1 : 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, n, m, k, j, f; pre(); fac[0] = fac[1] = 1; ifac[0] = ifac[1] = 1; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) fac[i] = (fac[i - 1] * 1LL * i) % mod; for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) ifac[i] = (ifac[i - 1] * 1LL * mpow(i, mod - 2)) % mod; int q; cin >> q; while (q--) { cin >> n >> f; if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; continue; } long long ans = 0; for (int x : g[n]) { ans += (mu[n / x] * ff(x, f)) % mod; if (ans < 0) ans += mod; if (ans >= mod) ans -= mod; } cout << ans << endl; } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((long long)result * base) % mod; base = ((long long)base * base) % mod; exp >>= 1; } return result; } ```
#include <bits/stdc++.h> using namespace std; int fact[1000000], d[1000000], l; inline int Pow_Log(int x, int p) { int sol = 1; while (p) { if (p & 1) { sol = (1LL * sol * x) % 1000000007; --p; } p >>= 1; x = (1LL * x * x) % 1000000007; } return sol; } inline int Comb(int n, int k) { int aux = (1LL * fact[n] * Pow_Log(fact[k], 1000000007 - 2)) % 1000000007; return (1LL * aux * Pow_Log(fact[n - k], 1000000007 - 2)) % 1000000007; } inline int Nr(int n, int k) { if (n < 0) return 0; return Comb(n + k - 1, k - 1); } inline void Diviz(int x) { l = 0; int k = 0; while (x % 2 == 0) { x >>= 1; ++k; } if (k) d[++l] = 2; int dd = 3; while (1LL * dd * dd <= x && x > 1) { k = 0; while (x % dd == 0) { ++k; x /= dd; } if (k) d[++l] = dd; dd += 2; } if (x > 1) d[++l] = x; } inline void Fix(int &x) { if (x >= 1000000007) x -= 1000000007; if (x < 0) x += 1000000007; } int main() { int n, f, i, j, Q, sol; cin.sync_with_stdio(0); cin >> Q; fact[0] = 1; for (i = 1; i <= 100000; ++i) fact[i] = (1LL * fact[i - 1] * i) % 1000000007; while (Q--) { cin >> n >> f; sol = Nr(n - f, f); Diviz(n); for (i = 1; i < (1 << l); ++i) { int nr = 0, prod = 1; for (j = 0; j < l; ++j) if ((1 << j) & i) { ++nr; prod *= d[j + 1]; } if (nr & 1) sol -= Nr(n / prod - f, f); else sol += Nr(n / prod - f, f); Fix(sol); } cout << sol << "\n"; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int fact[1000000], d[1000000], l; inline int Pow_Log(int x, int p) { int sol = 1; while (p) { if (p & 1) { sol = (1LL * sol * x) % 1000000007; --p; } p >>= 1; x = (1LL * x * x) % 1000000007; } return sol; } inline int Comb(int n, int k) { int aux = (1LL * fact[n] * Pow_Log(fact[k], 1000000007 - 2)) % 1000000007; return (1LL * aux * Pow_Log(fact[n - k], 1000000007 - 2)) % 1000000007; } inline int Nr(int n, int k) { if (n < 0) return 0; return Comb(n + k - 1, k - 1); } inline void Diviz(int x) { l = 0; int k = 0; while (x % 2 == 0) { x >>= 1; ++k; } if (k) d[++l] = 2; int dd = 3; while (1LL * dd * dd <= x && x > 1) { k = 0; while (x % dd == 0) { ++k; x /= dd; } if (k) d[++l] = dd; dd += 2; } if (x > 1) d[++l] = x; } inline void Fix(int &x) { if (x >= 1000000007) x -= 1000000007; if (x < 0) x += 1000000007; } int main() { int n, f, i, j, Q, sol; cin.sync_with_stdio(0); cin >> Q; fact[0] = 1; for (i = 1; i <= 100000; ++i) fact[i] = (1LL * fact[i - 1] * i) % 1000000007; while (Q--) { cin >> n >> f; sol = Nr(n - f, f); Diviz(n); for (i = 1; i < (1 << l); ++i) { int nr = 0, prod = 1; for (j = 0; j < l; ++j) if ((1 << j) & i) { ++nr; prod *= d[j + 1]; } if (nr & 1) sol -= Nr(n / prod - f, f); else sol += Nr(n / prod - f, f); Fix(sol); } cout << sol << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int m = pow(10, 9) + 7; int fact[100001], rev_fact[100001]; vector<int> divisors[1000001]; int* num_of_dis; bool* check; int expmod(int a, int b) { int result = 1; while (b > 0) { if (b % 2 > 0) result = (result * (unsigned long long)a) % m; a = (a * (unsigned long long)a) % m; b /= 2; } return result; } int reverse_mod(int n) { return expmod(n, m - 2); } void inline prepare_data() { fact[0] = 1; rev_fact[0] = 1; for (int i = 1; i <= 100000; i++) { for (int j = i; j <= 100000; j += i) { divisors[j].push_back(i); } } for (int i = 1; i <= 100000; i++) { fact[i] = (fact[i - 1] * (unsigned long long)(i)) % m; rev_fact[i] = reverse_mod(fact[i]); } } int C(int n, int k) { if (k < 0 || k > n) return 0; else if (k == 0 || k == n) return 1; else { int result = (fact[n] * (unsigned long long)(rev_fact[k])) % m; result = (result * (unsigned long long)(rev_fact[n - k])) % m; return result; } } int num_of_distributions(int n, int f) { int result; if (f == n) result = 1; else if (f < 1 || f > n) result = 0; else { if (check[n]) return num_of_dis[n]; check[n] = true; result = C(n - 1, f - 1); for (int i = 0; i < divisors[n].size() - 1; i++) { result -= num_of_distributions(divisors[n][i], f); if (result < 0) result += m; } num_of_dis[n] = result; } return result; } int main() { int q; cin >> q; int n, f; prepare_data(); for (int i = 0; i < q; i++) { scanf("%d %d", &n, &f); num_of_dis = new int[n + 1]; check = new bool[n + 1]; for (int j = 0; j <= n; j++) check[j] = false; cout << num_of_distributions(n, f) << endl; delete (num_of_dis); delete (check); } }
### Prompt In Cpp, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int m = pow(10, 9) + 7; int fact[100001], rev_fact[100001]; vector<int> divisors[1000001]; int* num_of_dis; bool* check; int expmod(int a, int b) { int result = 1; while (b > 0) { if (b % 2 > 0) result = (result * (unsigned long long)a) % m; a = (a * (unsigned long long)a) % m; b /= 2; } return result; } int reverse_mod(int n) { return expmod(n, m - 2); } void inline prepare_data() { fact[0] = 1; rev_fact[0] = 1; for (int i = 1; i <= 100000; i++) { for (int j = i; j <= 100000; j += i) { divisors[j].push_back(i); } } for (int i = 1; i <= 100000; i++) { fact[i] = (fact[i - 1] * (unsigned long long)(i)) % m; rev_fact[i] = reverse_mod(fact[i]); } } int C(int n, int k) { if (k < 0 || k > n) return 0; else if (k == 0 || k == n) return 1; else { int result = (fact[n] * (unsigned long long)(rev_fact[k])) % m; result = (result * (unsigned long long)(rev_fact[n - k])) % m; return result; } } int num_of_distributions(int n, int f) { int result; if (f == n) result = 1; else if (f < 1 || f > n) result = 0; else { if (check[n]) return num_of_dis[n]; check[n] = true; result = C(n - 1, f - 1); for (int i = 0; i < divisors[n].size() - 1; i++) { result -= num_of_distributions(divisors[n][i], f); if (result < 0) result += m; } num_of_dis[n] = result; } return result; } int main() { int q; cin >> q; int n, f; prepare_data(); for (int i = 0; i < q; i++) { scanf("%d %d", &n, &f); num_of_dis = new int[n + 1]; check = new bool[n + 1]; for (int j = 0; j <= n; j++) check[j] = false; cout << num_of_distributions(n, f) << endl; delete (num_of_dis); delete (check); } } ```
#include <bits/stdc++.h> using namespace std; long long fact[200000], invf[200000]; long long mod_pow(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 1) return (a * mod_pow(a, b - 1)) % 1000000007LL; long long ans = mod_pow(a, b / 2); return (ans * ans) % 1000000007LL; } void gen_fact() { fact[0] = 1, invf[0] = 1; for (int i = 1; i < 200000; i++) { fact[i] = (fact[i - 1] * i) % 1000000007LL; invf[i] = mod_pow(fact[i], 1000000007LL - 2); } } long long choose(long long a, long long b) { if (a < b) return 0; return (((fact[a] * invf[b]) % 1000000007LL) * invf[a - b]) % 1000000007LL; } vector<long long> prime_factors(long long n) { vector<long long> a; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); while (n % i == 0) n /= i; } } if (n != 1) a.push_back(n); return a; } int pop_count(int i) { if (i <= 1) return i; return (i & 1) + pop_count(i >> 1); } long long solve(long long n, long long f) { vector<long long> a = prime_factors(n); int len = a.size(); long long tot = 0; for (int i = 0; i < (1 << len); i++) { long long uh = 1 - 2 * (pop_count(i) % 2); long long moo = n; for (int j = 0; j < len; j++) { if ((1 << j) & i) moo /= a[j]; } tot += uh * choose(moo - 1, f - 1); tot %= 1000000007LL; } if (tot < 0) tot += 1000000007LL; return tot; } int main() { gen_fact(); int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int n, f; scanf("%d %d", &n, &f); printf("%d\n", (int)solve(n, f)); } }
### Prompt Construct a CPP code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long fact[200000], invf[200000]; long long mod_pow(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 1) return (a * mod_pow(a, b - 1)) % 1000000007LL; long long ans = mod_pow(a, b / 2); return (ans * ans) % 1000000007LL; } void gen_fact() { fact[0] = 1, invf[0] = 1; for (int i = 1; i < 200000; i++) { fact[i] = (fact[i - 1] * i) % 1000000007LL; invf[i] = mod_pow(fact[i], 1000000007LL - 2); } } long long choose(long long a, long long b) { if (a < b) return 0; return (((fact[a] * invf[b]) % 1000000007LL) * invf[a - b]) % 1000000007LL; } vector<long long> prime_factors(long long n) { vector<long long> a; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); while (n % i == 0) n /= i; } } if (n != 1) a.push_back(n); return a; } int pop_count(int i) { if (i <= 1) return i; return (i & 1) + pop_count(i >> 1); } long long solve(long long n, long long f) { vector<long long> a = prime_factors(n); int len = a.size(); long long tot = 0; for (int i = 0; i < (1 << len); i++) { long long uh = 1 - 2 * (pop_count(i) % 2); long long moo = n; for (int j = 0; j < len; j++) { if ((1 << j) & i) moo /= a[j]; } tot += uh * choose(moo - 1, f - 1); tot %= 1000000007LL; } if (tot < 0) tot += 1000000007LL; return tot; } int main() { gen_fact(); int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int n, f; scanf("%d %d", &n, &f); printf("%d\n", (int)solve(n, f)); } } ```
#include <bits/stdc++.h> using namespace std; long long mul[100010]; long long in[100010]; long long v[100010]; void extend_gcd(long long a, long long b, long long &d, long long &x, long long &y) { if (!b) { d = a; x = 1; y = 0; } else { extend_gcd(b, a % b, d, y, x); y -= x * (a / b); } } long long inv(long long a, long long n) { long long d, x, y; extend_gcd(a, n, d, x, y); return d == 1 ? (x + n) % n : -1; } void init() { mul[0] = 1; in[0] = 1; for (int i = 1; i < 100010; ++i) { mul[i] = (mul[i - 1] * (long long)i) % 1000000007; in[i] = inv(mul[i], 1000000007); } v[1] = 1; v[2] = -1; v[3] = -1; for (int i = 4; i < 100010; ++i) { int tmp = i; int cnt = 0; for (int j = 2; j * j <= i; ++j) { if (tmp == 1) break; if (tmp % j == 0) { cnt++; tmp /= j; if (tmp % j == 0) { cnt = -1; } } if (cnt == -1) break; } if (cnt == -1) { v[i] = 0; } else { if (tmp != 1) cnt++; if (cnt % 2 == 1) v[i] = -1; else v[i] = 1; } } } long long calc(int n, int k) { if (n < 0) return 0; if (k > n) return 0; if (k < 0) return 0; long long ans = 1; ans = (ans * mul[n]) % 1000000007; ans = (ans * in[k]) % 1000000007; ans = (ans * in[n - k]) % 1000000007; return (ans + 1000000007) % 1000000007; } long long solve(int n, int f) { long long ans = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { long long tmp = v[n / i]; tmp = (1000000007 + tmp * calc(i - 1, f - 1)) % 1000000007; ans = (ans + tmp) % 1000000007; if (i * i != n) { tmp = v[i]; tmp = (1000000007 + tmp * calc((n / i) - 1, f - 1)) % 1000000007; ans = (ans + tmp) % 1000000007; } } } return ans; } int main() { init(); int Q; scanf("%d", &Q); while (Q--) { int n, f; scanf("%d%d", &n, &f); printf("%lld\n", solve(n, f)); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mul[100010]; long long in[100010]; long long v[100010]; void extend_gcd(long long a, long long b, long long &d, long long &x, long long &y) { if (!b) { d = a; x = 1; y = 0; } else { extend_gcd(b, a % b, d, y, x); y -= x * (a / b); } } long long inv(long long a, long long n) { long long d, x, y; extend_gcd(a, n, d, x, y); return d == 1 ? (x + n) % n : -1; } void init() { mul[0] = 1; in[0] = 1; for (int i = 1; i < 100010; ++i) { mul[i] = (mul[i - 1] * (long long)i) % 1000000007; in[i] = inv(mul[i], 1000000007); } v[1] = 1; v[2] = -1; v[3] = -1; for (int i = 4; i < 100010; ++i) { int tmp = i; int cnt = 0; for (int j = 2; j * j <= i; ++j) { if (tmp == 1) break; if (tmp % j == 0) { cnt++; tmp /= j; if (tmp % j == 0) { cnt = -1; } } if (cnt == -1) break; } if (cnt == -1) { v[i] = 0; } else { if (tmp != 1) cnt++; if (cnt % 2 == 1) v[i] = -1; else v[i] = 1; } } } long long calc(int n, int k) { if (n < 0) return 0; if (k > n) return 0; if (k < 0) return 0; long long ans = 1; ans = (ans * mul[n]) % 1000000007; ans = (ans * in[k]) % 1000000007; ans = (ans * in[n - k]) % 1000000007; return (ans + 1000000007) % 1000000007; } long long solve(int n, int f) { long long ans = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { long long tmp = v[n / i]; tmp = (1000000007 + tmp * calc(i - 1, f - 1)) % 1000000007; ans = (ans + tmp) % 1000000007; if (i * i != n) { tmp = v[i]; tmp = (1000000007 + tmp * calc((n / i) - 1, f - 1)) % 1000000007; ans = (ans + tmp) % 1000000007; } } } return ans; } int main() { init(); int Q; scanf("%d", &Q); while (Q--) { int n, f; scanf("%d%d", &n, &f); printf("%lld\n", solve(n, f)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100]; int q, n, f, prime[100100], sqr[100100], mu[100100], ans; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y & 1) ret = (ret * x) % MOD; x = (x * x) % MOD, y >>= 1; } return ret; } long long C(int x, int y) { if (y > x) return 0; return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD; } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD; if (ans < 0) ans += MOD; else if (ans >= MOD) ans -= MOD; } printf("%d\n", ans); } return 0; }
### Prompt In CPP, your task is to solve the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long long ff[100100], inv_ff[100100]; int q, n, f, prime[100100], sqr[100100], mu[100100], ans; vector<int> divi[100100]; long long inv_mod(long long x) { long long ret = 1LL, y = MOD - 2; while (y) { if (y & 1) ret = (ret * x) % MOD; x = (x * x) % MOD, y >>= 1; } return ret; } long long C(int x, int y) { if (y > x) return 0; return (ff[x] * ((inv_ff[y] * inv_ff[x - y]) % MOD)) % MOD; } void precompute() { inv_ff[0] = 1LL; ff[0] = 1LL; for (int i = 1; i <= 100099; i++) ff[i] = (ff[i - 1] * i) % MOD; for (int i = 1; i <= 100099; i++) inv_ff[i] = inv_mod(ff[i]); for (int i = 1; i <= 100099; i++) { for (int j = i; j <= 100099; j += i) { divi[j].push_back(i); } } for (int i = 0; i <= 100099; i++) prime[i] = 1; prime[0] = 0, prime[1] = 0; for (int i = 2; i <= 100099; i++) { if (prime[i] == 1) { for (int j = i + i; j <= 100099; j += i) prime[j] = 0; } } for (int i = 2; i * i <= 100099; i++) { int t = i * i; for (int j = t; j <= 100099; j += t) sqr[j] = 1; } for (int i = 1; i <= 100099; i++) { if (sqr[i] == 1) continue; int cnt = 0; for (__typeof(divi[i].begin()) d(divi[i].begin()); d != divi[i].end(); d++) if (prime[*d] == 1) cnt++; mu[i] = (cnt % 2 == 1) ? -1 : 1; } return; } int main() { precompute(); scanf("%d", &q); while (q--) { scanf("%d%d", &n, &f); if (n == 1 and f == 1) { printf("1\n"); continue; } if (n > 1 and f == 1) { printf("0\n"); continue; } ans = 0; for (__typeof(divi[n].begin()) d(divi[n].begin()); d != divi[n].end(); d++) { ans = (ans + mu[*d] * C((n / (*d)) - 1, f - 1)) % MOD; if (ans < 0) ans += MOD; else if (ans >= MOD) ans -= MOD; } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; int pri[20], fac[100005], rfac[100005]; int mo[40005], A[40005]; int fast(int a, int b) { int sum = 1; while (b > 0) { if (b & 1) sum = 1ll * sum * a % P; a = 1ll * a * a % P; b >>= 1; } return sum; } void add(int &a, int b) { a += b; if (a >= P) a -= P; } void del(int &a, int b) { a -= b; if (a < 0) a += P; } int C(int n, int m) { if (m > n) return 0; return 1ll * fac[n] * rfac[m] % P * rfac[n - m] % P; } void solve(int K, int y) { int n = 0, len = 0; int x = K; for (int i = 1; i * i <= K; i++) { if (K % i == 0) { A[++n] = i; if (i * i != K) A[++n] = K / i; } if (i != 1 && x % i == 0) { pri[++len] = i; do { x /= i; } while (x % i == 0); } } if (x > 1) pri[++len] = x; mo[1] = 1; for (int i = 2; i <= n; i++) { int x = A[i], tot = 0; bool flag = 1; for (int j = 1; j <= len; j++) { int cnt = 0; if (x % pri[j] == 0) { tot++; do { x /= pri[j]; cnt++; if (cnt == 2) flag = 0; } while (x % pri[j] == 0 && flag); } if (!flag) break; } if (!flag) mo[i] = 0; else { if (tot & 1) mo[i] = -1; else mo[i] = 1; } } int ans = 0; for (int i = 1; i <= n; i++) { if (mo[i] == 1) add(ans, C(K / A[i] - 1, y - 1)); else if (mo[i] == -1) del(ans, C(K / A[i] - 1, y - 1)); } printf("%d\n", ans); } int main() { fac[0] = rfac[0] = 1; for (int i = 1; i <= 100000; i++) { fac[i] = 1ll * fac[i - 1] * i % P; rfac[i] = fast(fac[i], P - 2); } int q, n, m; scanf("%d", &q); while (q--) { scanf("%d %d", &n, &m); solve(n, m); } return 0; }
### Prompt Generate a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; int pri[20], fac[100005], rfac[100005]; int mo[40005], A[40005]; int fast(int a, int b) { int sum = 1; while (b > 0) { if (b & 1) sum = 1ll * sum * a % P; a = 1ll * a * a % P; b >>= 1; } return sum; } void add(int &a, int b) { a += b; if (a >= P) a -= P; } void del(int &a, int b) { a -= b; if (a < 0) a += P; } int C(int n, int m) { if (m > n) return 0; return 1ll * fac[n] * rfac[m] % P * rfac[n - m] % P; } void solve(int K, int y) { int n = 0, len = 0; int x = K; for (int i = 1; i * i <= K; i++) { if (K % i == 0) { A[++n] = i; if (i * i != K) A[++n] = K / i; } if (i != 1 && x % i == 0) { pri[++len] = i; do { x /= i; } while (x % i == 0); } } if (x > 1) pri[++len] = x; mo[1] = 1; for (int i = 2; i <= n; i++) { int x = A[i], tot = 0; bool flag = 1; for (int j = 1; j <= len; j++) { int cnt = 0; if (x % pri[j] == 0) { tot++; do { x /= pri[j]; cnt++; if (cnt == 2) flag = 0; } while (x % pri[j] == 0 && flag); } if (!flag) break; } if (!flag) mo[i] = 0; else { if (tot & 1) mo[i] = -1; else mo[i] = 1; } } int ans = 0; for (int i = 1; i <= n; i++) { if (mo[i] == 1) add(ans, C(K / A[i] - 1, y - 1)); else if (mo[i] == -1) del(ans, C(K / A[i] - 1, y - 1)); } printf("%d\n", ans); } int main() { fac[0] = rfac[0] = 1; for (int i = 1; i <= 100000; i++) { fac[i] = 1ll * fac[i - 1] * i % P; rfac[i] = fast(fac[i], P - 2); } int q, n, m; scanf("%d", &q); while (q--) { scanf("%d %d", &n, &m); solve(n, m); } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { int f = 1; x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } const int N = 100000 + 5; const int mo = 1000000007; int T, n, k; int vis[N]; long long fac[N], inv[N]; long long f[N]; vector<int> v[N]; void exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, y, x); y -= x * (a / b); } void pre() { int K = 100000; long long y; fac[0] = 1; for (int i = 1; i <= K; i++) fac[i] = fac[i - 1] * i % mo; for (int i = 0; i <= K; i++) exgcd(fac[i], mo, inv[i], y); for (int i = 2; i <= K; i++) { for (int j = i; j <= K; j += i) { v[j].push_back(i); } } } long long C(int a, int b) { if (b > a) return 0; long long res = fac[a] * inv[b] % mo * inv[a - b] % mo; return res; } long long calc(int x) { if (vis[x] == T) return f[x]; long long tmp = C(x - 1, k - 1); for (int i = 0; i < v[x].size(); i++) (tmp -= calc(x / v[x][i])) %= mo; f[x] = tmp; vis[x] = T; return f[x]; } int main() { read(T); pre(); memset(vis, 0, sizeof(vis)); while (T) { read(n), read(k); printf("%lld\n", (calc(n) + mo) % mo); T--; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { int f = 1; x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } const int N = 100000 + 5; const int mo = 1000000007; int T, n, k; int vis[N]; long long fac[N], inv[N]; long long f[N]; vector<int> v[N]; void exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, y, x); y -= x * (a / b); } void pre() { int K = 100000; long long y; fac[0] = 1; for (int i = 1; i <= K; i++) fac[i] = fac[i - 1] * i % mo; for (int i = 0; i <= K; i++) exgcd(fac[i], mo, inv[i], y); for (int i = 2; i <= K; i++) { for (int j = i; j <= K; j += i) { v[j].push_back(i); } } } long long C(int a, int b) { if (b > a) return 0; long long res = fac[a] * inv[b] % mo * inv[a - b] % mo; return res; } long long calc(int x) { if (vis[x] == T) return f[x]; long long tmp = C(x - 1, k - 1); for (int i = 0; i < v[x].size(); i++) (tmp -= calc(x / v[x][i])) %= mo; f[x] = tmp; vis[x] = T; return f[x]; } int main() { read(T); pre(); memset(vis, 0, sizeof(vis)); while (T) { read(n), read(k); printf("%lld\n", (calc(n) + mo) % mo); T--; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MOD = 1000000007; long long mu[MAXN], pr[MAXN]; long long fact[MAXN], inv[MAXN]; void compute_mu() { for (int i = 1; i < MAXN; i++) mu[i] = -1, pr[i] = 1; for (int i = 2; i < MAXN; i++) { if (pr[i]) { mu[i] = 1; for (int j = 2; i * j < MAXN; j++) { if (j % i == 0) mu[i * j] = 0; else mu[i * j] *= -1; pr[i * j] = 0; } } } } long long mod_pow(long long a, long long b, long long m) { long long ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % m; a = a * a % m; } return ans; } long long ncr(long long n, long long r) { long long ans = fact[n] * inv[r] % MOD * inv[n - r] % MOD; return ans; } void pre() { fact[0] = inv[0] = 1; for (int i = 1; i < MAXN; i++) fact[i] = fact[i - 1] * i % MOD; inv[MAXN - 1] = mod_pow(fact[MAXN - 1], MOD - 2, MOD); for (int i = MAXN - 2; i; i--) inv[i] = inv[i + 1] * (i + 1) % MOD; compute_mu(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); pre(); int q; cin >> q; while (q--) { long long n, f; cin >> n >> f; long long ans = 0; for (int i = 1, j = n; i * i <= n; i++, j = n / i) { if (n % i != 0) continue; if (i != 1 && j >= f) { ans = (ans + mu[i] * ncr(j - 1, f - 1) + MOD) % MOD; } if (i != j && i >= f) { ans = (ans + mu[j] * ncr(i - 1, f - 1) + MOD) % MOD; } } ans = (ncr(n - 1, f - 1) - ans + MOD) % MOD; cout << ans << '\n'; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MOD = 1000000007; long long mu[MAXN], pr[MAXN]; long long fact[MAXN], inv[MAXN]; void compute_mu() { for (int i = 1; i < MAXN; i++) mu[i] = -1, pr[i] = 1; for (int i = 2; i < MAXN; i++) { if (pr[i]) { mu[i] = 1; for (int j = 2; i * j < MAXN; j++) { if (j % i == 0) mu[i * j] = 0; else mu[i * j] *= -1; pr[i * j] = 0; } } } } long long mod_pow(long long a, long long b, long long m) { long long ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % m; a = a * a % m; } return ans; } long long ncr(long long n, long long r) { long long ans = fact[n] * inv[r] % MOD * inv[n - r] % MOD; return ans; } void pre() { fact[0] = inv[0] = 1; for (int i = 1; i < MAXN; i++) fact[i] = fact[i - 1] * i % MOD; inv[MAXN - 1] = mod_pow(fact[MAXN - 1], MOD - 2, MOD); for (int i = MAXN - 2; i; i--) inv[i] = inv[i + 1] * (i + 1) % MOD; compute_mu(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); pre(); int q; cin >> q; while (q--) { long long n, f; cin >> n >> f; long long ans = 0; for (int i = 1, j = n; i * i <= n; i++, j = n / i) { if (n % i != 0) continue; if (i != 1 && j >= f) { ans = (ans + mu[i] * ncr(j - 1, f - 1) + MOD) % MOD; } if (i != j && i >= f) { ans = (ans + mu[j] * ncr(i - 1, f - 1) + MOD) % MOD; } } ans = (ncr(n - 1, f - 1) - ans + MOD) % MOD; cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int w = 1e5; std::map<std::pair<int, int>, int> st; std::vector<int> d[w + 10]; int a[w + 10]; int ft[w + 10]; inline int pmod(int a, int b) { int res = 1; while (b) { if (b & 1) res = (1ll * res * a) % mod; a = (1ll * a * a) % mod; b >>= 1; } return res; } inline int calc(int n, int k) { if (k > n) return 0; return (1ll * ft[n] * pmod((1ll * ft[k] * ft[n - k]) % mod, mod - 2)) % mod; } int solve(int n, int m) { if (m < n) return 0; std::pair<int, int> x = std::pair<int, int>(n, m); if (st.find(x) != st.end()) return st[x]; long long res = calc(m - 1, n - 1); int sz = int(d[m].size()); for (int i = 0; i < sz; ++i) res -= solve(n, d[m][i]); return st[x] = res % mod; } int main() { ft[0] = 1; for (int i = 1; i <= w; ++i) ft[i] = (1ll * ft[i - 1] * i) % mod; for (int i = 2; i <= w; ++i) d[i].push_back(1); for (int i = 2; i << 1 <= w; ++i) for (int j = i << 1; j <= w; j += i) d[j].push_back(i); int tt; for (scanf("%d", &tt); tt--;) { int n, m; scanf("%d %d", &m, &n); int ans = (solve(n, m) % mod + mod) % mod; printf("%d\n", ans); } }
### Prompt Your task is to create a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> const int mod = 1e9 + 7; const int w = 1e5; std::map<std::pair<int, int>, int> st; std::vector<int> d[w + 10]; int a[w + 10]; int ft[w + 10]; inline int pmod(int a, int b) { int res = 1; while (b) { if (b & 1) res = (1ll * res * a) % mod; a = (1ll * a * a) % mod; b >>= 1; } return res; } inline int calc(int n, int k) { if (k > n) return 0; return (1ll * ft[n] * pmod((1ll * ft[k] * ft[n - k]) % mod, mod - 2)) % mod; } int solve(int n, int m) { if (m < n) return 0; std::pair<int, int> x = std::pair<int, int>(n, m); if (st.find(x) != st.end()) return st[x]; long long res = calc(m - 1, n - 1); int sz = int(d[m].size()); for (int i = 0; i < sz; ++i) res -= solve(n, d[m][i]); return st[x] = res % mod; } int main() { ft[0] = 1; for (int i = 1; i <= w; ++i) ft[i] = (1ll * ft[i - 1] * i) % mod; for (int i = 2; i <= w; ++i) d[i].push_back(1); for (int i = 2; i << 1 <= w; ++i) for (int j = i << 1; j <= w; j += i) d[j].push_back(i); int tt; for (scanf("%d", &tt); tt--;) { int n, m; scanf("%d %d", &m, &n); int ans = (solve(n, m) % mod + mod) % mod; printf("%d\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; int mu[100010], lst[100010], can[100010], flag[100010], pr[100010]; const long long Mod = 1000000007LL; long long fac[100010], inv[100010]; long long exp(int a, long long b, long long c) { long long ans = 1, num = a; while (b > 0) { if (b % 2 == 1) ans = ans * num % c; num = num * num % c; b >>= 1; } return ans; } long long cal(int n, int m) { if (n < m) return 0; long long ans = fac[n]; ans = ans * inv[m] % Mod; ans = ans * inv[n - m] % Mod; return ans; } int main() { memset(flag, -1, sizeof(flag)); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= 100000; i++) { fac[i] = fac[i - 1] * i % Mod; inv[i] = exp(i, Mod - 2, Mod) * inv[i - 1] % Mod; } mu[1] = 1; int pcnt = 0; for (int i = 2; i <= 100000; i++) { if (flag[i]) { pr[pcnt++] = i; lst[i] = i; mu[i] = -1; } for (int j = 0; j < pcnt && pr[j] <= 100000 / i; j++) { flag[i * pr[j]] = 0; if (lst[i] == pr[j]) { mu[i * pr[j]] = 0; break; } mu[i * pr[j]] = -mu[i]; lst[i * pr[j]] = pr[j]; } } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); long long ans = 0; for (int j = 1; j <= x / j; j++) { if (x % j != 0) continue; if (x / j == j) { if (mu[j]) ans += cal(j - 1, y - 1) * mu[j]; } else { if (mu[x / j]) ans += cal(j - 1, y - 1) * mu[x / j]; if (mu[j]) ans += cal(x / j - 1, y - 1) * mu[j]; } ans %= Mod; } printf("%I64d\n", (ans + Mod) % Mod); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int mu[100010], lst[100010], can[100010], flag[100010], pr[100010]; const long long Mod = 1000000007LL; long long fac[100010], inv[100010]; long long exp(int a, long long b, long long c) { long long ans = 1, num = a; while (b > 0) { if (b % 2 == 1) ans = ans * num % c; num = num * num % c; b >>= 1; } return ans; } long long cal(int n, int m) { if (n < m) return 0; long long ans = fac[n]; ans = ans * inv[m] % Mod; ans = ans * inv[n - m] % Mod; return ans; } int main() { memset(flag, -1, sizeof(flag)); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= 100000; i++) { fac[i] = fac[i - 1] * i % Mod; inv[i] = exp(i, Mod - 2, Mod) * inv[i - 1] % Mod; } mu[1] = 1; int pcnt = 0; for (int i = 2; i <= 100000; i++) { if (flag[i]) { pr[pcnt++] = i; lst[i] = i; mu[i] = -1; } for (int j = 0; j < pcnt && pr[j] <= 100000 / i; j++) { flag[i * pr[j]] = 0; if (lst[i] == pr[j]) { mu[i * pr[j]] = 0; break; } mu[i * pr[j]] = -mu[i]; lst[i * pr[j]] = pr[j]; } } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); long long ans = 0; for (int j = 1; j <= x / j; j++) { if (x % j != 0) continue; if (x / j == j) { if (mu[j]) ans += cal(j - 1, y - 1) * mu[j]; } else { if (mu[x / j]) ans += cal(j - 1, y - 1) * mu[x / j]; if (mu[j]) ans += cal(x / j - 1, y - 1) * mu[j]; } ans %= Mod; } printf("%I64d\n", (ans + Mod) % Mod); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; int const N = 1e5 + 20, mod = 1e9 + 7; int q, n, f; int fac[N], rev[N]; bool np[N]; vector<int> vec[N]; inline void rel(int &a) { if (a < 0) a += mod; if (a >= mod) a -= mod; } inline int pw(int a, int b) { int res = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) res = 1ll * res * a % mod; return res; } inline int inv(int a) { return pw(a, mod - 2); } inline int Comb(int n, int k) { if (k < 0 || k > n) return 0; return 1ll * fac[n] * rev[k] % mod * rev[n - k] % mod; } void pre() { np[0] = np[1] = 1; for (int i = 2; i < N; i++) { if (np[i]) continue; for (int j = 2 * i; j < N; j += i) np[j] = 1; } for (int i = 1; i < N; i++) vec[i].push_back(1); for (int i = 2; i < N; i++) { if (np[i]) continue; for (int j = i; j < N; j += i) { int sz = (int)vec[j].size(); for (int k = 0; k < sz; k++) vec[j].push_back(-vec[j][k] * i); } } fac[0] = rev[0] = 1; for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod, rev[i] = inv(fac[i]); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); pre(); cin >> q; while (q--) { cin >> n >> f; int ans = 0; for (int k : vec[n]) { int a = abs(k), b = (k < 0 ? -1 : 1); ans += b * Comb(n / a - 1, f - 1); rel(ans); } cout << ans << '\n'; } }
### Prompt Please create a solution in cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; int const N = 1e5 + 20, mod = 1e9 + 7; int q, n, f; int fac[N], rev[N]; bool np[N]; vector<int> vec[N]; inline void rel(int &a) { if (a < 0) a += mod; if (a >= mod) a -= mod; } inline int pw(int a, int b) { int res = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) res = 1ll * res * a % mod; return res; } inline int inv(int a) { return pw(a, mod - 2); } inline int Comb(int n, int k) { if (k < 0 || k > n) return 0; return 1ll * fac[n] * rev[k] % mod * rev[n - k] % mod; } void pre() { np[0] = np[1] = 1; for (int i = 2; i < N; i++) { if (np[i]) continue; for (int j = 2 * i; j < N; j += i) np[j] = 1; } for (int i = 1; i < N; i++) vec[i].push_back(1); for (int i = 2; i < N; i++) { if (np[i]) continue; for (int j = i; j < N; j += i) { int sz = (int)vec[j].size(); for (int k = 0; k < sz; k++) vec[j].push_back(-vec[j][k] * i); } } fac[0] = rev[0] = 1; for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod, rev[i] = inv(fac[i]); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); pre(); cin >> q; while (q--) { cin >> n >> f; int ans = 0; for (int k : vec[n]) { int a = abs(k), b = (k < 0 ? -1 : 1); ans += b * Comb(n / a - 1, f - 1); rel(ans); } cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long mod = 1e9 + 7; long long fact[N], inv[N]; long long dp[N]; int done[N]; int q; int id; long long n, f; vector<long long> prime; vector<long long> g[N]; long long mobius[N]; bool composite[N]; void mobi() { memset(composite, false, sizeof composite); mobius[1] = 1; for (int i = 2; i < N; i++) { if (!composite[i]) { prime.push_back(i); mobius[i] = -1; } for (int j = 0; j < prime.size() and i * prime[j] < N; j++) { composite[i * prime[j]] = true; if (i % prime[j] == 0) { mobius[i * prime[j]] = 0; break; } else { mobius[i * prime[j]] = mobius[i] * mobius[prime[j]]; } } } } long long bigmod(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } void pre() { fact[0] = 1, inv[0] = bigmod(1, mod - 2); for (int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = bigmod(fact[i], mod - 2); for (int j = i; j < N; j += i) { g[j].push_back(i); } } } long long ncr(long long n, long long r) { if (n < r or r < 0) return 0; long long up = (fact[n] * inv[r]) % mod; up = (up * inv[n - r]) % mod; return up; } long long go(long long n, long long f) { long long res = 0; for (auto x : g[n]) { res = (res + mobius[n / x] * ncr(x - 1, f - 1)) % mod; if (res < 0) res += mod; } return res; } int main() { mobi(); pre(); scanf("%d", &q); while (q--) { scanf("%lld %lld", &n, &f); printf("%lld\n", go(n, f)); } }
### Prompt Your task is to create a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long mod = 1e9 + 7; long long fact[N], inv[N]; long long dp[N]; int done[N]; int q; int id; long long n, f; vector<long long> prime; vector<long long> g[N]; long long mobius[N]; bool composite[N]; void mobi() { memset(composite, false, sizeof composite); mobius[1] = 1; for (int i = 2; i < N; i++) { if (!composite[i]) { prime.push_back(i); mobius[i] = -1; } for (int j = 0; j < prime.size() and i * prime[j] < N; j++) { composite[i * prime[j]] = true; if (i % prime[j] == 0) { mobius[i * prime[j]] = 0; break; } else { mobius[i * prime[j]] = mobius[i] * mobius[prime[j]]; } } } } long long bigmod(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } void pre() { fact[0] = 1, inv[0] = bigmod(1, mod - 2); for (int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = bigmod(fact[i], mod - 2); for (int j = i; j < N; j += i) { g[j].push_back(i); } } } long long ncr(long long n, long long r) { if (n < r or r < 0) return 0; long long up = (fact[n] * inv[r]) % mod; up = (up * inv[n - r]) % mod; return up; } long long go(long long n, long long f) { long long res = 0; for (auto x : g[n]) { res = (res + mobius[n / x] * ncr(x - 1, f - 1)) % mod; if (res < 0) res += mod; } return res; } int main() { mobi(); pre(); scanf("%d", &q); while (q--) { scanf("%lld %lld", &n, &f); printf("%lld\n", go(n, f)); } } ```
#include <bits/stdc++.h> using namespace std; const long long N = 200000; long long mod = 1e9 + 7; long long fac[N]; long long power(long long n, long long k) { if (k == 0) { return 1; } else { if (k % 2 == 0) { long long x = power(n, k / 2); return x * x % mod; } else { long long x = power(n, k / 2); x = x * x % mod; return x * n % mod; } } } long long fm[N]; long long ent(long long k, long long n) { if (k > n) { return 0; } if (k == 0 || k == n) { return 1; } return (((fac[n] * fm[k]) % mod) * fm[n - k]) % mod; } long long dp[N]; long long choose(long long n, long long f) { return ent(f - 1, n - 1); } long long za[N]; long long mark[N]; long long p[N]; int32_t main() { for (long long i = 2; i < N; i++) { if (p[i] == 0) { p[i] = i; for (long long j = i * i; j < N; j += i) { p[j] = i; } } else { if (i % (p[i] * p[i]) == 0) { mark[i] = 1; } if (mark[i / p[i]] == 1) { mark[i] = 1; } } za[i] = 1 - za[i / p[i]]; } fac[0] = 1; for (long long i = 1; i < N; i++) { fac[i] = (fac[i - 1] * i) % mod; } for (long long i = 0; i < N; i++) { fm[i] = power(fac[i], mod - 2); } long long q; cin >> q; for (long long i = 0; i < q; i++) { long long n, f; cin >> n >> f; if (f == 1) { if (n == 1) { cout << 1 << endl; continue; } cout << 0 << endl; continue; } long long ans = choose(n, f); ans = (ans + mod) % mod; for (long long j = 2; j * j <= n; j++) { if (n % j == 0) { if (!mark[j]) { if (za[j] == 1) { ans -= choose(n / j, f); ans = (ans % mod + mod) % mod; } else { ans += choose(n / j, f); ans = (ans % mod + mod) % mod; } } long long m = n / j; if (!mark[m]) { if (m != j) { if (za[m] == 1) { ans -= choose(n / m, f); ans = (ans + mod) % mod; } else { ans += choose(n / m, f); ans = (ans % mod + mod) % mod; } } } } } cout << ans << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 200000; long long mod = 1e9 + 7; long long fac[N]; long long power(long long n, long long k) { if (k == 0) { return 1; } else { if (k % 2 == 0) { long long x = power(n, k / 2); return x * x % mod; } else { long long x = power(n, k / 2); x = x * x % mod; return x * n % mod; } } } long long fm[N]; long long ent(long long k, long long n) { if (k > n) { return 0; } if (k == 0 || k == n) { return 1; } return (((fac[n] * fm[k]) % mod) * fm[n - k]) % mod; } long long dp[N]; long long choose(long long n, long long f) { return ent(f - 1, n - 1); } long long za[N]; long long mark[N]; long long p[N]; int32_t main() { for (long long i = 2; i < N; i++) { if (p[i] == 0) { p[i] = i; for (long long j = i * i; j < N; j += i) { p[j] = i; } } else { if (i % (p[i] * p[i]) == 0) { mark[i] = 1; } if (mark[i / p[i]] == 1) { mark[i] = 1; } } za[i] = 1 - za[i / p[i]]; } fac[0] = 1; for (long long i = 1; i < N; i++) { fac[i] = (fac[i - 1] * i) % mod; } for (long long i = 0; i < N; i++) { fm[i] = power(fac[i], mod - 2); } long long q; cin >> q; for (long long i = 0; i < q; i++) { long long n, f; cin >> n >> f; if (f == 1) { if (n == 1) { cout << 1 << endl; continue; } cout << 0 << endl; continue; } long long ans = choose(n, f); ans = (ans + mod) % mod; for (long long j = 2; j * j <= n; j++) { if (n % j == 0) { if (!mark[j]) { if (za[j] == 1) { ans -= choose(n / j, f); ans = (ans % mod + mod) % mod; } else { ans += choose(n / j, f); ans = (ans % mod + mod) % mod; } } long long m = n / j; if (!mark[m]) { if (m != j) { if (za[m] == 1) { ans -= choose(n / m, f); ans = (ans + mod) % mod; } else { ans += choose(n / m, f); ans = (ans % mod + mod) % mod; } } } } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int di[maxn][300]; int usado, mk[maxn]; long long fa[maxn]; long long in[maxn]; long long dp[maxn]; int cont[maxn]; long long power(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2) return (power(a, b - 1) * a) % 1000000007; long long t = power(a, b / 2); return (t * t) % 1000000007; } long long F(int n, int f) { if (n == f) return 1; if (n < f) return 0; if (mk[n] == usado) return dp[n]; long long ans = 0; mk[n] = usado; for (int i = 1; i <= cont[n]; i++) { int x = di[n][i]; ans = (ans + F(n / x, f)) % 1000000007; } return dp[n] = ans = (((((fa[n - 1] * in[n - 1 - f + 1]) % 1000000007) * in[f - 1]) % 1000000007) + 1000000007 - ans) % 1000000007; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 2; i <= maxn - 5; i++) { for (int j = i; j <= maxn - 5; j += i) { cont[j]++; di[j][cont[j]] = i; } } fa[0] = 1; in[0] = power(1, 1000000007 - 2); for (int i = 1; i <= 100000; i++) { fa[i] = (fa[i - 1] * i) % 1000000007; in[i] = power(fa[i], 1000000007 - 2); } int q; cin >> q; int n, f; while (q--) { cin >> n >> f; usado++; cout << F(n, f) << '\n'; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100005; int di[maxn][300]; int usado, mk[maxn]; long long fa[maxn]; long long in[maxn]; long long dp[maxn]; int cont[maxn]; long long power(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2) return (power(a, b - 1) * a) % 1000000007; long long t = power(a, b / 2); return (t * t) % 1000000007; } long long F(int n, int f) { if (n == f) return 1; if (n < f) return 0; if (mk[n] == usado) return dp[n]; long long ans = 0; mk[n] = usado; for (int i = 1; i <= cont[n]; i++) { int x = di[n][i]; ans = (ans + F(n / x, f)) % 1000000007; } return dp[n] = ans = (((((fa[n - 1] * in[n - 1 - f + 1]) % 1000000007) * in[f - 1]) % 1000000007) + 1000000007 - ans) % 1000000007; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 2; i <= maxn - 5; i++) { for (int j = i; j <= maxn - 5; j += i) { cont[j]++; di[j][cont[j]] = i; } } fa[0] = 1; in[0] = power(1, 1000000007 - 2); for (int i = 1; i <= 100000; i++) { fa[i] = (fa[i - 1] * i) % 1000000007; in[i] = power(fa[i], 1000000007 - 2); } int q; cin >> q; int n, f; while (q--) { cin >> n >> f; usado++; cout << F(n, f) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int MOD = 1000000007; map<pair<long long, long long>, long long> dp; long long A[MAX], B[MAX]; long long Qmod(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1LL) res = res * x % MOD; x = x * x % MOD; n >>= 1LL; } return res; } void init() { A[0] = 1; B[0] = 1; for (int i = 1; i < MAX; i++) { A[i] = (A[i - 1] * i) % MOD; B[i] = Qmod(A[i], (long long)(MOD - 2)); } } long long dfs(long long n, long long f) { if (dp.find(make_pair(n, f)) != dp.end()) return dp[make_pair(n, f)]; if (n < f) return 0; if (f == 1) return n == 1 ? 1 : 0; long long t = ((A[n - 1] * B[f - 1]) % MOD * B[n - f]) % MOD; long long i; for (i = 2; i * i < n; i++) { if (n % i == 0) { t = (t - dfs(n / i, f) - dfs(i, f)) % MOD; } } if (i * i == n) t = (t - dfs(n / i, f)) % MOD; return dp[make_pair(n, f)] = t; } int main() { int cas; long long n, f; dp.clear(); init(); scanf("%d", &cas); for (int i = 0; i < cas; ++i) { scanf("%I64d %I64d", &n, &f); printf("%I64d\n", (dfs(n, f) + MOD) % MOD); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int MOD = 1000000007; map<pair<long long, long long>, long long> dp; long long A[MAX], B[MAX]; long long Qmod(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1LL) res = res * x % MOD; x = x * x % MOD; n >>= 1LL; } return res; } void init() { A[0] = 1; B[0] = 1; for (int i = 1; i < MAX; i++) { A[i] = (A[i - 1] * i) % MOD; B[i] = Qmod(A[i], (long long)(MOD - 2)); } } long long dfs(long long n, long long f) { if (dp.find(make_pair(n, f)) != dp.end()) return dp[make_pair(n, f)]; if (n < f) return 0; if (f == 1) return n == 1 ? 1 : 0; long long t = ((A[n - 1] * B[f - 1]) % MOD * B[n - f]) % MOD; long long i; for (i = 2; i * i < n; i++) { if (n % i == 0) { t = (t - dfs(n / i, f) - dfs(i, f)) % MOD; } } if (i * i == n) t = (t - dfs(n / i, f)) % MOD; return dp[make_pair(n, f)] = t; } int main() { int cas; long long n, f; dp.clear(); init(); scanf("%d", &cas); for (int i = 0; i < cas; ++i) { scanf("%I64d %I64d", &n, &f); printf("%I64d\n", (dfs(n, f) + MOD) % MOD); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { int nxt, t; } A[1166900]; int head[100010], node_cnt; void add_edge(int x, int y) { A[node_cnt] = (node){head[x], y}; head[x] = node_cnt++; } int pri[100010], pri_cnt, mu[100010]; bool mark[100010]; void sieve() { int i, j; mu[1] = 1; for (i = 2; i < 100010; i++) { if (!mark[i]) { pri[++pri_cnt] = i; mu[i] = -1; } for (j = 1; j <= pri_cnt; j++) { int t = i * pri[j]; if (t >= 100010) { break; } mark[t] = 1; if ((i % pri[j]) == 0) { mu[t] = 0; break; } mu[t] = -mu[i]; } } memset(head, -1, sizeof(head)); for (i = 100010 - 1; i >= 1; i--) { for (j = i; j < 100010; j += i) { add_edge(j, i); } } } int Fac[100010], Inv[100010], InvFac[100010]; void init() { sieve(); int i; Fac[0] = 1; for (i = 1; i < 100010; i++) { Fac[i] = 1LL * Fac[i - 1] * i % 1000000007; } Inv[1] = 1; for (i = 2; i < 100010; i++) { Inv[i] = 1LL * (1000000007 - 1000000007 / i) * Inv[1000000007 % i] % 1000000007; } InvFac[0] = 1; for (i = 1; i < 100010; i++) { InvFac[i] = 1LL * InvFac[i - 1] * Inv[i] % 1000000007; } } int C(int n, int m) { if (n < m) { return 0; } return 1LL * Fac[n] * InvFac[n - m] % 1000000007 * InvFac[m] % 1000000007; } int main() { init(); int Case; scanf("%d", &Case); while (Case--) { int i, n, m, Ans = 0; scanf("%d%d", &n, &m); for (i = head[n]; ~i; i = A[i].nxt) { int x = A[i].t, y = n / x; if (y < m) { break; } Ans = (Ans + mu[x] * C(y - 1, m - 1)) % 1000000007; } if (Ans < 0) { Ans += 1000000007; } printf("%d\n", Ans); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int nxt, t; } A[1166900]; int head[100010], node_cnt; void add_edge(int x, int y) { A[node_cnt] = (node){head[x], y}; head[x] = node_cnt++; } int pri[100010], pri_cnt, mu[100010]; bool mark[100010]; void sieve() { int i, j; mu[1] = 1; for (i = 2; i < 100010; i++) { if (!mark[i]) { pri[++pri_cnt] = i; mu[i] = -1; } for (j = 1; j <= pri_cnt; j++) { int t = i * pri[j]; if (t >= 100010) { break; } mark[t] = 1; if ((i % pri[j]) == 0) { mu[t] = 0; break; } mu[t] = -mu[i]; } } memset(head, -1, sizeof(head)); for (i = 100010 - 1; i >= 1; i--) { for (j = i; j < 100010; j += i) { add_edge(j, i); } } } int Fac[100010], Inv[100010], InvFac[100010]; void init() { sieve(); int i; Fac[0] = 1; for (i = 1; i < 100010; i++) { Fac[i] = 1LL * Fac[i - 1] * i % 1000000007; } Inv[1] = 1; for (i = 2; i < 100010; i++) { Inv[i] = 1LL * (1000000007 - 1000000007 / i) * Inv[1000000007 % i] % 1000000007; } InvFac[0] = 1; for (i = 1; i < 100010; i++) { InvFac[i] = 1LL * InvFac[i - 1] * Inv[i] % 1000000007; } } int C(int n, int m) { if (n < m) { return 0; } return 1LL * Fac[n] * InvFac[n - m] % 1000000007 * InvFac[m] % 1000000007; } int main() { init(); int Case; scanf("%d", &Case); while (Case--) { int i, n, m, Ans = 0; scanf("%d%d", &n, &m); for (i = head[n]; ~i; i = A[i].nxt) { int x = A[i].t, y = n / x; if (y < m) { break; } Ans = (Ans + mu[x] * C(y - 1, m - 1)) % 1000000007; } if (Ans < 0) { Ans += 1000000007; } printf("%d\n", Ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 30, Mod = 1e9 + 7; const long long SQ = 330; long long dp[N]; vector<long long> Div[N]; long long f[N], I[N]; bool vis[N]; inline long long _sub(long long x, long long y) { long long res = x - y; if (res < 0) res += Mod; res %= Mod; return res; } inline long long _mul(long long x, long long y) { return (x * y) % Mod; } long long _pow(long long x, long long y) { if (!x) return 0; if (!y) return 1; long long t = _pow(x, y / 2); t *= t; t %= Mod; if (y % 2 == 1) return _mul(t, x); return t; } inline void pre() { for (int i = 1; i < N; i++) { for (int j = i * 2; j < N; j += i) Div[j].push_back(i); } f[0] = 1; for (int i = 1; i < N; i++) f[i] = _mul(f[i - 1], i); I[N - 1] = _pow(f[N - 1], Mod - 2); for (int i = N - 2; i > 0; i--) { I[i] = _mul(i + 1, I[i + 1]); } } inline long long C(long long r, long long n) { if (r == 0) return 1; if (r > n or n < 0) return 0; if (r == n) return 1; return _mul(_mul(f[n], I[r]), I[n - r]); } long long solve(long long n, long long f) { if (f > n) return 0; if (vis[n]) return dp[n]; vis[n] = 1; long long res = C(f - 1, n - 1); for (auto d : Div[n]) { res = _sub(res, solve(d, f)); } return dp[n] = res; } int main() { pre(); long long q; scanf("%lld", &q); while (q--) { long long n, f; scanf("%lld%lld", &n, &f); if (n < f) { cout << 0 << "\n"; continue; } for (auto u : Div[n]) dp[u] = 0, vis[u] = 0; dp[n] = 0, vis[n] = 0; long long p = solve(n, f); printf("%lld", p); printf("\n"); } return (0); }
### Prompt Please create a solution in cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 30, Mod = 1e9 + 7; const long long SQ = 330; long long dp[N]; vector<long long> Div[N]; long long f[N], I[N]; bool vis[N]; inline long long _sub(long long x, long long y) { long long res = x - y; if (res < 0) res += Mod; res %= Mod; return res; } inline long long _mul(long long x, long long y) { return (x * y) % Mod; } long long _pow(long long x, long long y) { if (!x) return 0; if (!y) return 1; long long t = _pow(x, y / 2); t *= t; t %= Mod; if (y % 2 == 1) return _mul(t, x); return t; } inline void pre() { for (int i = 1; i < N; i++) { for (int j = i * 2; j < N; j += i) Div[j].push_back(i); } f[0] = 1; for (int i = 1; i < N; i++) f[i] = _mul(f[i - 1], i); I[N - 1] = _pow(f[N - 1], Mod - 2); for (int i = N - 2; i > 0; i--) { I[i] = _mul(i + 1, I[i + 1]); } } inline long long C(long long r, long long n) { if (r == 0) return 1; if (r > n or n < 0) return 0; if (r == n) return 1; return _mul(_mul(f[n], I[r]), I[n - r]); } long long solve(long long n, long long f) { if (f > n) return 0; if (vis[n]) return dp[n]; vis[n] = 1; long long res = C(f - 1, n - 1); for (auto d : Div[n]) { res = _sub(res, solve(d, f)); } return dp[n] = res; } int main() { pre(); long long q; scanf("%lld", &q); while (q--) { long long n, f; scanf("%lld%lld", &n, &f); if (n < f) { cout << 0 << "\n"; continue; } for (auto u : Div[n]) dp[u] = 0, vis[u] = 0; dp[n] = 0, vis[n] = 0; long long p = solve(n, f); printf("%lld", p); printf("\n"); } return (0); } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int fac[100001], inv[100001]; vector<int> e[100001]; bool v[100001]; int cnt, p[100001], mu[100001]; inline int ksm(long long base, int expo) { long long ret = 1; for (; expo; (base *= base) %= mod, expo >>= 1) if (expo & 1) (ret *= base) %= mod; return ret; } inline int C(int n, int m) { return n < m ? 0 : (int)((long long)fac[n] * inv[m] % mod * inv[n - m] % mod); } int main() { mu[1] = fac[0] = 1; for (int i = 1; i <= 100000; i++) e[i].push_back(1), fac[i] = (long long)fac[i - 1] * i % mod; inv[100000] = ksm(fac[100000], mod - 2); for (int i = 99999; i >= 0; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % mod; for (int i = 2; i <= 100000; i++) { if (!v[i]) p[++cnt] = i, mu[i] = -1; if (mu[i]) { for (int j = i; j <= 100000; j += i) e[j].push_back(i); } for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) { v[i * p[j]] = 1; if (i % p[j] == 0) break; mu[i * p[j]] = -mu[i]; } } int q; scanf("%d", &q); while (q--) { int n, k; scanf("%d%d", &n, &k); int ans = 0; for (int i : e[n]) (ans += C(n / i - 1, k - 1) * mu[i]) %= mod; if (ans < 0) ans += mod; printf("%d\n", ans); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int fac[100001], inv[100001]; vector<int> e[100001]; bool v[100001]; int cnt, p[100001], mu[100001]; inline int ksm(long long base, int expo) { long long ret = 1; for (; expo; (base *= base) %= mod, expo >>= 1) if (expo & 1) (ret *= base) %= mod; return ret; } inline int C(int n, int m) { return n < m ? 0 : (int)((long long)fac[n] * inv[m] % mod * inv[n - m] % mod); } int main() { mu[1] = fac[0] = 1; for (int i = 1; i <= 100000; i++) e[i].push_back(1), fac[i] = (long long)fac[i - 1] * i % mod; inv[100000] = ksm(fac[100000], mod - 2); for (int i = 99999; i >= 0; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % mod; for (int i = 2; i <= 100000; i++) { if (!v[i]) p[++cnt] = i, mu[i] = -1; if (mu[i]) { for (int j = i; j <= 100000; j += i) e[j].push_back(i); } for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) { v[i * p[j]] = 1; if (i % p[j] == 0) break; mu[i * p[j]] = -mu[i]; } } int q; scanf("%d", &q); while (q--) { int n, k; scanf("%d%d", &n, &k); int ans = 0; for (int i : e[n]) (ans += C(n / i - 1, k - 1) * mu[i]) %= mod; if (ans < 0) ans += mod; printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int p[100010], cnt, miu[100010]; bool vis[100010]; long long fac[100010], inv[100010], facinv[100010]; void init() { miu[1] = 1; for (int i = 2; i <= 100000; i++) { if (!vis[i]) { p[++cnt] = i; miu[i] = -1; } for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) { vis[i * p[j]] = 1; if (i % p[j] == 0) { miu[i * p[j]] = 0; break; } miu[i * p[j]] = -miu[i]; } } fac[0] = 1; for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % mod; inv[1] = 1; for (int i = 2; i <= 100000; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; facinv[0] = 1; for (int i = 1; i <= 100000; i++) facinv[i] = facinv[i - 1] * inv[i] % mod; } long long C(long long n, long long k) { if (k < 0 || k > n) return 0; if (k == 0 || k == n) return 1; return fac[n] * facinv[k] % mod * facinv[n - k] % mod; } long long cal(int n, int f) { return C(n - 1, f - 1); } void solve(int n, int f) { long long ans = 0; for (int i = 1; i * i <= n; i++) if (n % i == 0) { ans += miu[i] * cal(n / i, f) % mod; if (i * i != n) ans += miu[n / i] * cal(i, f) % mod; ans %= mod; } cout << (ans + mod) % mod << endl; } int main() { init(); int q; cin >> q; while (q--) { int n, f; scanf("%d%d", &n, &f); solve(n, f); } return 0; }
### Prompt Create a solution in cpp for the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int p[100010], cnt, miu[100010]; bool vis[100010]; long long fac[100010], inv[100010], facinv[100010]; void init() { miu[1] = 1; for (int i = 2; i <= 100000; i++) { if (!vis[i]) { p[++cnt] = i; miu[i] = -1; } for (int j = 1; j <= cnt && i * p[j] <= 100000; j++) { vis[i * p[j]] = 1; if (i % p[j] == 0) { miu[i * p[j]] = 0; break; } miu[i * p[j]] = -miu[i]; } } fac[0] = 1; for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % mod; inv[1] = 1; for (int i = 2; i <= 100000; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; facinv[0] = 1; for (int i = 1; i <= 100000; i++) facinv[i] = facinv[i - 1] * inv[i] % mod; } long long C(long long n, long long k) { if (k < 0 || k > n) return 0; if (k == 0 || k == n) return 1; return fac[n] * facinv[k] % mod * facinv[n - k] % mod; } long long cal(int n, int f) { return C(n - 1, f - 1); } void solve(int n, int f) { long long ans = 0; for (int i = 1; i * i <= n; i++) if (n % i == 0) { ans += miu[i] * cal(n / i, f) % mod; if (i * i != n) ans += miu[n / i] * cal(i, f) % mod; ans %= mod; } cout << (ans + mod) % mod << endl; } int main() { init(); int q; cin >> q; while (q--) { int n, f; scanf("%d%d", &n, &f); solve(n, f); } return 0; } ```
#include <bits/stdc++.h> long long ans, f[100010], i, tot, g[100010], a, b, c, n, j; void exgcd(long long a, long long b, long long &x, long long &y) { long long t; if (b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, x, y); t = x; x = y; y = t - a / b * y; } long long cnt(int a, int b) { long long u, v, w, x, y; u = f[a]; w = 1000000007; v = (f[b] * f[a - b]) % 1000000007; exgcd(v, w, x, y); x = (x % 1000000007 + 1000000007) % 1000000007; x = (x * u) % 1000000007; return x; } void dfs(long long x, long long y, long long z) { long long i; if (a / x - 1 < b - 1) return; if (z == 0) ans = (ans + cnt(a / x - 1, b - 1)) % 1000000007; else ans = (ans - cnt(a / x - 1, b - 1) + 1000000007) % 1000000007; for (i = y; i <= tot; i++) if (x * g[i] <= a) dfs(x * g[i], i + 1, 1 - z); } int main() { scanf("%I64d", &n); f[0] = 1; for (i = 1; i <= 100000; i++) f[i] = f[i - 1] * i % 1000000007; for (i = 1; i <= n; i++) { ans = 0; scanf("%I64d%I64d", &a, &b); c = a; tot = 0; for (j = 2; j <= sqrt(a); j++) if (c % j == 0) { tot++; g[tot] = j; while (c % j == 0) c = c / j; } if (c > 1) { tot++; g[tot] = c; } dfs(1, 1, 0); printf("%I64d\n", ans); } }
### Prompt Please create a solution in Cpp to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> long long ans, f[100010], i, tot, g[100010], a, b, c, n, j; void exgcd(long long a, long long b, long long &x, long long &y) { long long t; if (b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, x, y); t = x; x = y; y = t - a / b * y; } long long cnt(int a, int b) { long long u, v, w, x, y; u = f[a]; w = 1000000007; v = (f[b] * f[a - b]) % 1000000007; exgcd(v, w, x, y); x = (x % 1000000007 + 1000000007) % 1000000007; x = (x * u) % 1000000007; return x; } void dfs(long long x, long long y, long long z) { long long i; if (a / x - 1 < b - 1) return; if (z == 0) ans = (ans + cnt(a / x - 1, b - 1)) % 1000000007; else ans = (ans - cnt(a / x - 1, b - 1) + 1000000007) % 1000000007; for (i = y; i <= tot; i++) if (x * g[i] <= a) dfs(x * g[i], i + 1, 1 - z); } int main() { scanf("%I64d", &n); f[0] = 1; for (i = 1; i <= 100000; i++) f[i] = f[i - 1] * i % 1000000007; for (i = 1; i <= n; i++) { ans = 0; scanf("%I64d%I64d", &a, &b); c = a; tot = 0; for (j = 2; j <= sqrt(a); j++) if (c % j == 0) { tot++; g[tot] = j; while (c % j == 0) c = c / j; } if (c > 1) { tot++; g[tot] = c; } dfs(1, 1, 0); printf("%I64d\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const long long MOD = 1e9 + 7; long long fact[N]; long long invfact[N]; long long mod_pow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } inline long long inv(long long x) { return mod_pow(x, MOD - 2); } inline long long comb(long long n, long long r) { return (fact[n] * invfact[r]) % MOD * invfact[n - r] % MOD; } vector<int> factor[N]; map<int, long long> dp[N]; int rec(int f, int n) { if (f < n) return 0; if (dp[n].count(f)) return dp[n][f]; long long &res = dp[n][f]; res = comb(f - 1, n - 1); for (auto e : factor[f]) { res -= rec(e, n); if (res < 0) res += MOD; } return res; } int main() { ios::sync_with_stdio(false); for (int i = 2; i < N; ++i) { for (int j = i * 2; j < N; j += i) factor[j].push_back(i); } fact[0] = 1; for (int i = (1); i < (int)(N); ++i) fact[i] = (fact[i - 1] * i) % MOD; for (int i = (0); i < (int)(N); ++i) invfact[i] = inv(fact[i]); int q; cin >> q; for (int i = (0); i < (int)(q); ++i) { int f, n; cin >> f >> n; if (n == 1) cout << (f == 1) << '\n'; else cout << rec(f, n) << '\n'; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const long long MOD = 1e9 + 7; long long fact[N]; long long invfact[N]; long long mod_pow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } inline long long inv(long long x) { return mod_pow(x, MOD - 2); } inline long long comb(long long n, long long r) { return (fact[n] * invfact[r]) % MOD * invfact[n - r] % MOD; } vector<int> factor[N]; map<int, long long> dp[N]; int rec(int f, int n) { if (f < n) return 0; if (dp[n].count(f)) return dp[n][f]; long long &res = dp[n][f]; res = comb(f - 1, n - 1); for (auto e : factor[f]) { res -= rec(e, n); if (res < 0) res += MOD; } return res; } int main() { ios::sync_with_stdio(false); for (int i = 2; i < N; ++i) { for (int j = i * 2; j < N; j += i) factor[j].push_back(i); } fact[0] = 1; for (int i = (1); i < (int)(N); ++i) fact[i] = (fact[i - 1] * i) % MOD; for (int i = (0); i < (int)(N); ++i) invfact[i] = inv(fact[i]); int q; cin >> q; for (int i = (0); i < (int)(q); ++i) { int f, n; cin >> f >> n; if (n == 1) cout << (f == 1) << '\n'; else cout << rec(f, n) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } const double PI = 2 * acos(0.0); const double eps = 1e-9; const int infi = 1e9; const long long Linfi = (long long)1e18; const long long MOD = 1000000007; int n, f; int sieve[100005], cnt[100005]; long long gt[100005], gt_inverse[100005]; long long power(long long a, long long p) { if (p == 0) return 1; if (p == 1) return a; long long ans = power(a, p / 2); ans = (ans * ans) % MOD; if (p % 2 == 1) ans = (ans * a) % MOD; return ans; } long long tohop(int n, int k) { if (k < 0 || k > n) return 0; long long ans = (gt[n] * gt_inverse[k]) % MOD; ans = (ans * gt_inverse[n - k]) % MOD; return ans; } void Init() { memset(sieve, 0, sizeof(sieve)); memset(cnt, 0, sizeof(cnt)); sieve[1] = 1; for (int i = 2; i <= sqrt(100000); i++) { if (sieve[i] == 0) for (int j = i * i; j <= 100000; j += i) sieve[j] = i; } for (int i = 2; i <= 100000; i++) { int tmp = i; while (sieve[tmp]) { int t = tmp / sieve[tmp]; if (t % sieve[tmp] == 0) { cnt[i] = -1; break; } tmp = t; cnt[i]++; } if (cnt[i] >= 0) cnt[i]++; } gt[0] = 1; for (int i = 1; i <= 100000; i++) gt[i] = (gt[i - 1] * i) % MOD; for (int i = 0; i <= 100000; i++) gt_inverse[i] = power(gt[i], MOD - 2); } void update(long long &res, int a, int f, int x) { if (cnt[x] < 0) return; if (cnt[x] % 2 == 0) res = (res + tohop(a - 1, f - 1)) % MOD; else res = (res - tohop(a - 1, f - 1) + MOD) % MOD; } void solve() { if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; return; } long long res = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { update(res, n / i, f, i); if (i * i != n) update(res, i, f, n / i); } } cout << res << endl; } int main() { ios::sync_with_stdio(false); Init(); int sotest; cin >> sotest; while (sotest--) { cin >> n >> f; solve(); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7). To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7). Input The first line contains an integer q representing the number of queries (1 ≀ q ≀ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≀ f ≀ n ≀ 105). Output For each query, output a single integer in a line corresponding to the answer of each query. Examples Input 5 6 2 7 2 6 3 6 4 7 4 Output 2 6 9 10 20 Note For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1]. For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } const double PI = 2 * acos(0.0); const double eps = 1e-9; const int infi = 1e9; const long long Linfi = (long long)1e18; const long long MOD = 1000000007; int n, f; int sieve[100005], cnt[100005]; long long gt[100005], gt_inverse[100005]; long long power(long long a, long long p) { if (p == 0) return 1; if (p == 1) return a; long long ans = power(a, p / 2); ans = (ans * ans) % MOD; if (p % 2 == 1) ans = (ans * a) % MOD; return ans; } long long tohop(int n, int k) { if (k < 0 || k > n) return 0; long long ans = (gt[n] * gt_inverse[k]) % MOD; ans = (ans * gt_inverse[n - k]) % MOD; return ans; } void Init() { memset(sieve, 0, sizeof(sieve)); memset(cnt, 0, sizeof(cnt)); sieve[1] = 1; for (int i = 2; i <= sqrt(100000); i++) { if (sieve[i] == 0) for (int j = i * i; j <= 100000; j += i) sieve[j] = i; } for (int i = 2; i <= 100000; i++) { int tmp = i; while (sieve[tmp]) { int t = tmp / sieve[tmp]; if (t % sieve[tmp] == 0) { cnt[i] = -1; break; } tmp = t; cnt[i]++; } if (cnt[i] >= 0) cnt[i]++; } gt[0] = 1; for (int i = 1; i <= 100000; i++) gt[i] = (gt[i - 1] * i) % MOD; for (int i = 0; i <= 100000; i++) gt_inverse[i] = power(gt[i], MOD - 2); } void update(long long &res, int a, int f, int x) { if (cnt[x] < 0) return; if (cnt[x] % 2 == 0) res = (res + tohop(a - 1, f - 1)) % MOD; else res = (res - tohop(a - 1, f - 1) + MOD) % MOD; } void solve() { if (f == 1) { if (n == 1) cout << 1 << endl; else cout << 0 << endl; return; } long long res = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { update(res, n / i, f, i); if (i * i != n) update(res, i, f, n / i); } } cout << res << endl; } int main() { ios::sync_with_stdio(false); Init(); int sotest; cin >> sotest; while (sotest--) { cin >> n >> f; solve(); } return 0; } ```