組合數學 1的個數

2021-09-06 18:00:06 字數 796 閱讀 7409

mean:

輸入乙個n,計算小於10^n的正整數中含有1的數的個數。

analyse:

這題是一道組合數學課後思考題。

基本思路:  組合數學乘法原則 + 容斥原理

n位數中,每位可選:,所以共有10^n種,其中要除掉每位都為0的情況,所以要減一。

其中每位上不選1的情況為:,所以共有9^n中,同樣要除掉全部為0的情況。

time complexity:

o(n)

source code:

//memory   time

// 1347k 0ms

// by : snarl_jsb

#include#include#include#include#include#include#include#include#include#include#include#include#define n 1000010

#define ll long long

using namespace std;

//輸入乙個n,求小於10^n的正整數中有多少個1;

int main()

sol--;

int res=1;

for(int i=1;i<=n;i++)

res--;

cout<}

return 0;

}

組合數學 求組合數

對於求組合數,要根據所給資料範圍來選擇合適的演算法 這道題中所給的資料範圍適合用打表的方法直接暴力求解 先用4e6的複雜度預處理出所有的情況,再用1e4的複雜度完成詢問即可 include using namespace std const int n 2010 const int mod 1e9 ...

數學 組合數學

mod must be a prime const int mod 1e9 7 namespace combinatory ll inv ll x ll fac maxn invfac maxn void initc int n ll a ll n,ll m ll c ll n,ll m ll d ...

1 組合數學 幻方

幻方 對角線,每一行,每一列加起來都相等 幻和 每一行或列或對角線數字的和 1 2 3 n2 n2 n2 1 2 所以每一行的和為n n2 1 2 構造幻方 奇數階幻方 連續擺放法 雙偶數階 4k 幻方 對稱法 單偶數 4k 2 幻方 斯特雷奇法 連續擺放法 擺1在第一行第 n 1 2列 行號 1,...