暴力DFS+验证。验证如果是o(n)检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理。#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
usingnamespace std;constint maxn=20;
bool flag[maxn];
bool r;
int a[maxn];
int n,sum;int mul[15][15];bool check(int s)
{if(s==sum) return1;return0;
}void dfs(int deep,int tot)
{if(deep==n-1){if(check(tot)){r=1;for(int i=0;i<=deep;i++){...
数学题。弄懂了之久其实就是解一个一元二次方程 x*(x-1)/2=y 。如果y==0了。表明刚好是这个数。不是的话,就x取大一位然后 y-n 再%9 。不过记得 %9==0的时候是9。不会出现0。#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long ...
找规律!求N!最后非0位的值。比方2是120的最后一个不是0的值。
输入N比較大,要大数保存。
注意到最后0的个数是与5的因数的个数相等。设f(n)为n!的最后非0位。
那么f(n)=((n%5)!* f(n/5) *2^(n/5))%10
因数2的个数始终大于5,从1開始每连续5个划分为1组,当中5的倍数仅仅提取出一个因数5后,
组成一个新的数列1到n/5,我们有1*2*3*4*5=6*7*8*9*5=2(取最后一个非0位),这里就是2^(n/5)。
再乘上剩下来的几个数字就可以
(比...
来源:http://m.blog.csdn.net/article/details?id=70861055Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number of times each digit (0 to 9) appears in the sequence. Forexample, with N = 13, the sequence is: 12345678910111213In this sequence, 0appears once, 1 a...
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.#include <iostream>
#include <vector>
using namespace std;vector<int> int_vet(int a)
{vector<int> res;while (a){int tmp = a % 10;a /= 10;res.push_back(tmp);}return res;
}int a[10] = { 0 }...
题目链接我的思路略复杂,这里介绍一个比较简洁的做法。对于 $b \le \sqrt{N}$,暴力枚举 $b$。对于 $b > \sqrt{N}$, 注意到在 $b$ 进制下 $N$ 至多有 2 个数位,且最高位的取值小于 $\sqrt{N}$,此时可以暴力枚举最高位上的数字。扩展若数据范围扩大到 $10^{18}$ 该怎么做?原文:https://www.cnblogs.com/Patt/p/11902128.html
descptionWe have a sorted set of digits D, a non-empty subset of {‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘}. (Note that ‘0‘ is not included.)Now, we write numbers using these digits, using each digit as many times as we want. For example, if D = {‘1‘,‘3‘,‘5‘}, we may write numbers such as ‘13‘, ‘551‘, ‘1351315‘.Return the number of positive integers that can be written...
#include<cstdio>
#include<cstring>int num[3],tot,n,f;
char s[3][100],ans[3][100];int check()
{char str[100];int a=0,b=0,c;for(int i=0;i<num[0];i++)a=a*10+s[0][i]-'0';for(int i=0;i<num[1];i++)b=b*10+s[1][i]-'0';c=a*b;for(int i=num[2]-1;i>=0;i--){str[i]='0'+c%10;c/=10;}if(c>0||str[0]=='0')return 0;for(int i=0;i<num[2];i++)if(s[2][i]!=str[i]&&s[2][i]!='*')return 0;return 1;
}void dfs(int a,int b)
{if...
题目链接:huangjing思路:这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值。。。。具体实现详见代码。。。题目:Language:
Default
Backward Digit SumsTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4285 Accepted: 2474Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <...
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9518 Accepted Submission(s): 2758Problem Description假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912........
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.public class Solution {public int countDigitOne(int n) {long res = 0;int left = n;int right = 0;int base = 1;while (left > 0) {int cur = left % 10;left = left / 10;right = n % bas...
题目如下:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.Example:Input: 13
Output: 6
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.解题思路:本题是《编程之美》上的题目,详细分析如下图。代码如下:class Solution(object):def countDigitOne(self, n):""":type n: int:rtype: int"""if n <= 0:return 0res = 0sn...
1653: [Usaco2006 Feb]Backward Digit SumsTime Limit: 5 Sec Memory Limit: 64 MBSubmit: 207 Solved: 161[Submit][Status][Discuss]DescriptionFJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, on...
Leftmost DigitTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13680 Accepted Submission(s): 5239
Problem Description
Given a positive integer N, you should output the leftmost digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follo...
3404: [Usaco2009 Open]Cow Digit Game又见数字游戏Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 72 Solved: 48[Submit][Status][Discuss]Description 贝茜和约翰在玩一个数字游戏.贝茜需要你帮助她. 游戏一共进行了G(1≤G≤100)场.第i场游戏开始于一个正整数Ni(l≤Ni≤1,000,000).游戏规则是这样的:双方轮流操作,将当前的数字减去一个数,这个数可以是当前数字的最大数码,也可以是最小的非0数码.比如当前的数是...