zoukankan      html  css  js  c++  java
  • P2806 有趣的数 II

    有毒的数

    题目描述

    我们把一个数称为有趣的,当且仅当:

    1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次。

    2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前。

    3. 最高位数字不为0。

    因此,符合我们定义的最小的有趣的数是2013。除此以外,4位的有趣的数还有两个:2031和2301。

    请计算恰好有n位的有趣的数的个数。由于答案可能非常大,只需要输出答案除以1000000007的余数。

    输入输出格式

    输入格式:

    输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000)。

    输出格式:

    输出只有一行,包括恰好n 位的整数中有趣的数的个数除以1000000007的余数。

    输入输出样例

    输入样例#1: 
    4
    输出样例#1: 

    3

     ————————————————————————————————————————————————

    尝试两种方法。都只得了70分。Why?!

    code1

     1 #include <iostream>
     2  
     3 using namespace std;
     4  
     5 int main(){
     6     long mod = 1000000007;
     7     int n;
     8     cin>>n;
     9     long long **states = new long long*[n+1];
    10     for(int i =0;i<n+1;i++)
    11         states[i]=new long long[6];
    12     for(int i =0;i<6;i++)
    13         states[0][i]=0;
    14  
    15     for(int i=1;i<=n;i++)
    16     {
    17         int j = i-1;
    18         states[i][0] = 1;
    19         states[i][1] = (states[j][0] + states[j][1] * 2) % mod;
    20         states[i][2] = (states[j][0] + states[j][2]) % mod;
    21         states[i][3] = (states[j][1] + states[j][3] * 2) % mod;
    22         states[i][4] = (states[j][1] + states[j][2] + states[j][4] * 2) % mod;
    23         states[i][5] = (states[j][3] + states[j][4] + states[j][5] * 2) % mod;
    24     }
    25     cout<<states[n][5]<<endl;
    26     return 0;
    27 }

    code2

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 const int MOD = 1e9 + 7;
    10 int n;
    11 LL inv[1010];
    12 
    13 int main() {
    14     cin >> n;
    15     inv[1] = 1;
    16     for (int i = 2; i < 1000; ++ i)
    17         inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    18     LL ans = 0;
    19     LL col = n - 1;
    20     for (int i = 2; i <= n - 2; ++ i) {
    21         col = (col * (n-i) % MOD) * inv[i] % MOD;
    22         ans = (ans + (col * (i-1) * (n-i-1) % MOD)) % MOD;
    23     }
    24     cout << ans << endl;
    25     return 0;
    26 }

    急需一个说法。QWQ

  • 相关阅读:
    LeetCode: Number Complement
    LeetCode: Hamming Distance
    LeetCode: Sum of Left Leaves
    LeetCode: Ransom Note
    LeetCode: Minimum Moves to Equal Array Elements
    LeetCode: Linked List Random Node
    LeetCode: Product of Array Except Self
    LeetCode:Two Sum II
    LeetCode: Minimum Moves to Equal Array Elements II
    杂记 -- 时间段内应用程序调用次数限制
  • 原文地址:https://www.cnblogs.com/hkttg/p/9374402.html
Copyright © 2011-2022 走看看