# python3 def change(coins, n): len1 = len(coins) if len1 == 0 and n < 1 or n > 100000: return None ways = [0] * (n + 1) # 初始化 ways[0] = 1 for i in range(len1): for j in range(coins[i], n + 1): # 保证n小于等于100000,为了防止溢出,请将答案Mod 1000000007 ways[j] = (ways[j] + ways[j - coins[i]]) % 1000000007 print(ways[n]) if __name__ == '__main__': coins, n = [1, 2, 5, 10], int(input()) change(coins, n)
n = int(input()) m = [1] + [0]*n for i in (1,2,5,10): for j in range(i,n+1): m[j] = (m[j] + m[j-i]) % 1000000007 print(m[-1])
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = {1,2,5,10}; int[][] methodNum = new int[arr.length + 1][n + 1]; for(int i = 0;i <= arr.length;i++){ methodNum[i][0] = 1; } for(int i = 1;i <= arr.length;i++){ for(int j = 0;j <= n;j++){ if(j >= arr[i - 1]){ methodNum[i][j] = (methodNum[i - 1][j] + methodNum[i][j - arr[i - 1]]) % 1000000007; }else{ methodNum[i][j] = methodNum[i - 1][j]; } } } System.out.println(methodNum[arr.length][n]); } }
n = int(input()) n_5 = n//5 idea = 0 for x in range(n_5+1): idea += (x//2+1)*((n-5*x)//2+1) print(int(idea%(1e9+7)))运行时间: 46 ms 占用内存:3556K
function dealData(n){ var dp=new Array(100001); for(var k=0;k<dp.length;k++){ dp[k]=0; } dp[0]=1; var coins=[1,2,5,10]; for(var i=0;i<coins.length;i++){ for(var j=coins[i];j<dp.length;j++){ dp[j]=(dp[j]+dp[j-coins[i]])%1000000007 } } print(dp[n]); } dealData(readline());为什么在牛客网的编译器上,数组设为n+1不能通过?
#include<stdio.h> #include<math.h> int main() { long long n; long long coins[] = {1,2,5,10}; long long dp[1000001]; long long i, j; dp[0] = 1; scanf("%lld", &n); for (i = 0; i < 4; i++) { for (j = coins[i]; j <= n; j++) { dp[j] = (dp[j] + dp[j - coins[i]])%1000000007; } } printf("%lld", dp[n]); return 0; }
def change(coins, n): len1 = len(coins) if len1 == 0 and n < 1 or n > 100000: return None ways = [0] * (n + 1) # 初始化 ways[0] = 1 for i in range(len1): for j in range(coins[i], n + 1): # 保证n小于等于100000,为了防止溢出,请将答案Mod 1000000007 ways[j] = (ways[j] + ways[j - coins[i]]) % 1000000007 print(ways[n]) if __name__ == '__main__': coins, n = [1, 2, 5, 10], int(input()) change(coins, n) |