Safe Salesforce PasswordsSalesforce provides a sec...
創建於:2026年1月23日
使用 GPT-5.2 Thinking 回答 Chat01
創建於:2026年1月23日
使用 GPT-5.2 Thinking 回答 Chat01
Safe Salesforce PasswordsSalesforce provides a secure system for organizations to manage their accounts and data. To ensure maximum security, Salesforce has implemented password policies for user accounts. A password is considered safe if it satisfies the following criteria:The length of the password is exactly .The password consists of lowercase English characters only.The password does not contain or more consecutive identical characters.Given the integers (password length) and (maximum allowed consecutive identical characters), calculate the total number of distinct safe passwords that can be generated. Since the number can be large, return the result modulo .ExampleConsider .The total number of passwords of length 2 is . There are 26 cases where consecutive characters are the same. Thus, the answer is , and .Function DescriptionComplete the function countValidPasswords in the editor below.countValidPasswords has the following parameters:int n: the length of the passwordint k: the number of matching consecutive characters should be less than this numberReturnsint: the number of safe passwords, modulo Constraints2 \le n \le 10^5$$2 \le k \le nCode Editor (C++14)C++#include <bits/stdc++.h>
/*
int countValidPasswords(int n, int k) {
}
int main() ...give me cpp code
cpp#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; // Count length-n strings over 26 lowercase letters with no run of identical chars of length >= k int countValidPasswords(int n, int k) { vector<long long> dp(n + 1, 0); dp[0] = 1; // empty // For i < k, constraint is inactive => 26^i long long p26 = 1; int upto = min(n, k - 1); for (int i = 1; i <= upto; i++) { p26 = (p26 * 26LL) % MOD; dp[i] = p26; } if (n < k) return (int)dp[n]; // i == k: all strings minus the 26 strings of all-same letter dp[k] = (26LL * dp[k - 1] - 26LL) % MOD; if (dp[k] < 0) dp[k] += MOD; // i > k: dp[i] = 26*dp[i-1] - 25*dp[i-k] for (int i = k + 1; i <= n; i++) { dp[i] = (26LL * dp[i - 1] - 25LL * dp[i - k]) % MOD; if (dp[i] < 0) dp[i] += MOD; } return (int)dp[n]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; cout << countValidPasswords(n, k) << "\n"; return 0; }
xSalesforce Data CollectionA Salesforce administrator is working on optimizing data collection for Opportunity records across multiple branches.The branches follow a hierarchical structure, forming a tree with nodes labeled from 0 to . Each node represents a branch, and an array opportunityData of size indicates whether a branch contains important Opportunity data. Specifically, opportunityData[i] is 1 if branch holds relevant data and 0 otherwise.To gather all Opportunity data, the admin must navigate through the branches using bidirectional paths (edges), which can be traversed multiple times if necessary. The goal is to minimize the total distance traveled, measured by the number of edges crossed. Additionally, any data within a radius of 2 edges from a branch can be collected. For system consistency, the retrieval process must start and end at the same branch.Determine the minimum number of edges the admin needs to traverse to collect all Opportunity data.Function DescriptionComplete the function minEdgesToCollectData in the editor.The function has the following parameters:vector<int> opportunityData: An array indicating whether each branch contains Opportunity data.int branch_nodes: The number of branches in the hierarchy.vector<int> branch_from: The starting point of each connection.vector<int> branch_to: The ending point of each connection.Returns:int: The minimum number of edges required to collect all data.Constraints2 \le \text{branch\_nodes} \le 10^5$$\text{opportunityData}[i] = 0 or , where 0 \le i < \text{branch\_nodes}$$0 \le \text{branch\_from}[i] < \text{branch\_nodes} where 0 \le i < \text{branch\_nodes} - 1$$0 \le \text{branch\_to}[i] < \text{branch\_nodes} where Sample Case 1Sample Input:n = branch_nodes = 10opportunityData = [1, 0, 1, 1, 1, 1, 0, 0, 1, 1]Edges: (0,1), (0,2), (0,3), (0,4), (0,5), (1,6), (6,7), (7,8), (5,9)Sample Output:4Explanation:All Opportunity data, except the data at Branch 8, is collected from Branch 0. The admin follows the path , then collects the data at Branch 8 from Branch 6.int minEdgesToCollectData(vector<int> opportunityData, int branch_nodes, vector<int> branch_from, vector<int> branch_to) {
// Your code here
}give me cpp code
cpp#include <bits/stdc++.h> using namespace std; int minEdgesToCollectData(vector<int> opportunityData, int branch_nodes, vector<int> branch_from, vector<int> branch_to) { int n = branch_nodes; vector<vector<int>> adj(n); vector<int> deg(n, 0); for (int i = 0; i < n - 1; i++) { int u = branch_from[i], v = branch_to[i]; adj[u].push_back(v); adj[v].push_back(u); deg[u]++; deg[v]++; } vector<char> removed(n, 0); queue<int> q; // 1) Prune all leaves with no data (opportunityData[u] == 0) for (int i = 0; i < n; i++) { if (deg[i] == 1 && opportunityData[i] == 0) q.push(i); } while (!q.empty()) { int u = q.front(); q.pop(); if (removed[u]) continue; removed[u] = 1; for (int v : adj[u]) { if (removed[v]) continue; if (--deg[v] == 1 && opportunityData[v] == 0) q.push(v); } deg[u] = 0; } // 2) Remove two layers of remaining leaves (because you can collect within distance 2) for (int i = 0; i < n; i++) { if (!removed[i] && deg[i] == 1) q.push(i); } for (int layer = 0; layer < 2; layer++) { int sz = (int)q.size(); while (sz--) { int u = q.front(); q.pop(); if (removed[u]) continue; removed[u] = 1; for (int v : adj[u]) { if (removed[v]) continue; if (--deg[v] == 1) q.push(v); } deg[u] = 0; } } // 3) Remaining edges must be traversed twice (go and return) long long remainingEdgeCountTimes2 = 0; for (int i = 0; i < n; i++) { if (!removed[i]) remainingEdgeCountTimes2 += deg[i]; } long long remainingEdges = remainingEdgeCountTimes2 / 2; return (int)(2 * remainingEdges); }