Codeforces Round #491 (Div. 2) E - Bus Number + 反思

E - Bus Number

最近感觉打CF各种车祸。。。。。感觉要反思一下,

上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践,

这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong

就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法,

想了半天感觉时间来不及了就强行上了个java,结果时间来不及。。。

以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦。

import java.math.BigInteger;
import java.util.*;
public class Main {
    static long n;
    static int cnt[] = new int[10];
    static int num[] = new int[10];
    static long f[] = new long[20];
    static long ans = 0;
    static long one = 1000000000;
    static long base =  one * one;
    public static void main(String arg[]) {

        f[0] = 1;
        for(int i = 1; i <= 18; i++) {
            f[i] = f[i - 1] * i;
        }

        Scanner in = new Scanner(System.in);
        n = in.nextLong();
        if(n == base) {
            System.out.println("18");
        } else {
            while(n > 0) {
                int ret = (int)(n % 10);
                cnt[ret]++;
                n /= 10;
            }

            for(int i = 0; i < 10; i++) {
                if(cnt[i] > 0) {
                    num[i] = 1;
                    cnt[i]--;
                }
            }

            dfs(0);
            System.out.println(ans);
        }
        in.close();
    }

    public static void dfs(int p) {
        //System.out.println("#####");
        ans += cal();
        for(int i = p; i <= 9; i++) {
            if(cnt[i] == 0) continue;
            cnt[i]--;
            num[i]++;
            dfs(i);
            num[i]--;
            cnt[i]++;
        }
    }
    public static long cal() {
        int sum = 0;
        long ans = 0, ret = 0;
        for(int i = 0; i <= 9; i++) sum += num[i];
        ans = f[sum];

        for(int i = 0; i <= 9; i++) {
            if(num[i] > 0)ans /= f[num[i]];
        }

        if(num[0] > 0) {
            ret = f[sum - 1];
            ret /= f[num[0] - 1];
            for(int i = 1; i <= 9; i++) {
                if(num[i] > 0) {
                    ret /= f[num[i]];
                }
            }
        }
        return ans - ret;

    }
}

原文地址:https://www.cnblogs.com/CJLHY/p/9220407.html

时间: 2024-08-30 06:27:03

Codeforces Round #491 (Div. 2) E - Bus Number + 反思的相关文章

Codeforces Round #436 (Div. 2) C. Bus

Codeforces Round #436 (Div. 2) C. Bus A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the pointx = a, immediately turns back and then moves to the point x = 0. After re

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters B. Bus of Characters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In the Bus of Characters there are nn rows of seat, each having 22

Codeforces Round #491 (Div. 2) — 赛后补题

C. Candies PS:大概是又傻了,读题啊. #include<bits/stdc++.h> #define ll long long #define P pair<int,int> #define pb push_back #define lson root << 1 #define INF (int)2e9 + 7 #define maxn (int)1e5 + 7 #define rson root << 1 | 1 #define LINF (

【Codeforces】Codeforces Round #491 (Div. 2) (Contest 991)

题目 传送门:QWQ A:A - If at first you don't succeed... 分析: 按照题意模拟 代码: #include <bits/stdc++.h> using namespace std; int main(){ int a,b,c,n; scanf("%d%d%d%d",&a,&b,&c,&n); int ans=n-a-b+c; if(a<c || b<c) ans=-1; if(ans>

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs

Codeforces Round #480 (Div. 2) E. The Number Games

E. The Number Games time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented

Codeforces Round #481 (Div. 3) E. Bus Video System

E. Bus Video System Example 1 input 3 5 2 1 -3 output 3 Example 2 input 2 4 -1 1 output 4 Example 3 input 4 10 2 4 1 2 output 2 题目大意: 车上只会显示y-x(y是离开站时的人数,x时到站前的人数),问第一站的人数可以有几种 分析: 我们先分析下这个数学问题的式子,设a[i]=y[i]-x[i]--① x[i+1]=y[i],这是显然易见的 然后我们将①从1到n项累加

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We consider a positive integer perfect, if and only if the sum of its digits is exactly 1010. Given a positive integ

Codeforces Round #585 (Div. 2) B. The Number of Products

题目地址:http://codeforces.com/contest/1215/problem/B 题意:给你一个值都不为零的数组,分别找出有多少个连续的子串乘积小于零,大于零. 我的思路:先找出为正数的基础子串x个,即单个正数或两个负数连起来的子串算一个,可算得...算了一直wrong在样例7.还没想出来那错了,就不写了. 别人家的思路:从首开始找负数字串,即有一个负数后,在下一个负数前这里面的都是负数子串.而其他的就为正数子串,正数子串初始化应为1,所以得的正数子串最后应加上1.两个负数作为