日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVa10943

發布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVa10943 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

10943 How do you add?
Larry is very bad at math — he usually uses a calculator, which worked
well throughout college. Unforunately, he is now struck in a deserted
island with his good buddy Ryan after a snowboarding accident.
They’re now trying to spend some time figuring out some good
problems, and Ryan will eat Larry if he cannot answer, so his fate is
up to you!
It’s a very simple problem — given a number N, how many ways
can K numbers less than N add up to N?
For example, for N = 20 and K = 2, there are 21 ways:
0+20
1+19
2+18
3+17
4+16
5+15
...
18+2
19+1
20+0
Input
Each line will contain a pair of numbers N and K. N and K will both be an integer from 1 to 100,
inclusive. The input will terminate on 2 0’s.
Output
Since Larry is only interested in the last few digits of the answer, for each pair of numbers N and K,
print a single number mod 1,000,000 on a single line.
Sample Input
20 2
20 2
0 0
Sample Output
21
21

題意:

?????? 將K個不超過N的非負整數加起來,使得它們的和為N,有多少種方法?N=5,K=2時一共有6種方法,即0+5、1+4、2+3、3+2、4+1、5+0。輸出方法總數模1000000的余數。

分析:

?????? 相當于解方程sum{xi | i = 1,2,…,K && xi >= 0}。答案就是C(N+K-1,K-1)。

1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define ll long long 6 const int MOD = 1000000; 7 const int maxk = 200; 8 ll C[maxk + 2][maxk + 2]; 9 // 線性算法,可以加取模 10 void get_C(){ 11 memset(C,0,sizeof C); 12 C[0][0] = 1; 13 for(int i = 0 ; i <= maxk ; i++){ 14 C[i][0] = C[i][i] = 1; 15 for(int j = 1 ; j < i ; j++) 16 C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; 17 } 18 } 19 // 直接計算,不要隨便取模,計算量過大時會有誤差 20 long long cal_C(long long n,long long m){ 21 double ans = 1; 22 for(int i = 0 ; i < m ; i++) ans *= n - i; 23 for(int i = 0 ; i < m ; i++) ans /= i + 1; 24 return (long long)(ans + 0.5) % MOD; 25 } 26 int main(){ 27 int N,K; 28 get_C(); 29 while(scanf("%d%d",&N,&K) == 2 && N){ 30 printf("%lld\n",C[N + K - 1][K - 1]); 31 } 32 return 0; 33 } View Code

?

轉載于:https://www.cnblogs.com/cyb123456/p/5837669.html

總結

以上是生活随笔為你收集整理的UVa10943的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。