暴力搜索出奇迹— —第一场:答案数据较小

这里讲的这种暴搜是让题目不那么烦琐换种思路来解的一种方式。

B - 俾斯麦

设 f(n) 表示 n 的每一位数字的平方之和,求 [a,b] 中有几个 n 满足 k × f(n)=n

Input

第一行三个正整数 k, a, b

1 ≤ k, a, b ≤ 1018,且a ≤ b

Output

输出一个非负整数,表示解的个数

 

Sample Input

5 10 20

Sample Output

1

数位DP可以做,呼呼就是有点麻烦。(考试中照搬了以前一道算自幂数的数位DP嘻嘻)

想想满足条件的最大n是多少呢,假设这个n有18位,且每个位上都是9,那么左式也只等于81*18=910+648=1558,也就是n/k最多1558,答案最多也只有1558种,f(n)肯定是整数,所以枚举n/k就可以了,n虽然很大,但是n/k很小,枚举的时候判断一下符不符合条件就可以了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>

#define MOD 10000000
using namespace std; 
typedef long long LL;
typedef unsigned long long ULL;
ULL base=233;
void read(int &x)//'&'表示引用,也就是说x是一个实参,在函数中改变了x的值就意味着在外面x的值也会被改变
{
    int f=1;//标记正负
    x=0;//归零(这就是潜在bug,有可能传进来时x没有归零)
    char s=getchar();//读入第一个字符
    while(s<'0'||s>'9')//不是数字字符
    {
        if(s=='-')//不能直接把f=-1,有可能输入的不是'-'而是其他乱七八糟的东西
            f=-1;
        s=getchar();//继续读
    }
    while(s>='0'&&s<='9')//是字符(一旦不是字符就意味着输入结束了)
    {
        x=x*10+s-'0';
        s=getchar();
    }
    x*=f;//改变正负
}
long long quickpow(long long n, long long base) {
    long long res = 1;
    while(n) {
        if(n & 1) {
            res = res * base % MOD;
        }
        n >>= 1;
        base = base * base % MOD;
    }
    return res;
}//快速幂 
ULL HASH(char s[])
{
	int l=strlen(s);
	ULL ans=0;
	for(int i=0;i<l;i++)
	{
		ans=ans*base+(ULL)s[i];
	}
	return ans;
}
set<ULL>hash0;//手动哈希 
set<ULL>::iterator it;//STL迭代器 

set<int >set0;

LL cal(LL x)
{
	LL sum=0;
	while(x)
	{
		int y=x%10;
		sum+=y*y;
		x/=10;
	}
	return sum;
}
int main()
{
    LL k,a,b;
    cin>>k>>a>>b;
	LL ans=0;
	for(LL i=1;i<=1558;i++)
	{
	    LL answer=cal(i*k); 
	    if(answer==i&&i*k>=a&&i*k<=b)ans++;
	}
	cout<<ans;
}//1558*18(计算平方和有一个最多18的循环)10e5,给你0.01秒都能过。 










上面写用了1ms..羞耻。VJ好慢,去hiho上就是0ms。

B. Planning The Expedition

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Natasha is planning an expedition to Mars for nn people. One of the important tasks is to provide food for each participant.

The warehouse has mm daily food packages. Each package has some food type aiai .

Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.

Formally, for each participant jj Natasha should select his food type bjbj and each day jj -th participant will eat one food package of type bjbj . The values bjbj for different participants may be different.

What is the maximum possible number of days the expedition can last, following the requirements above?

Input

The first line contains two integers nn and mm (1≤n≤1001≤n≤100 , 1≤m≤1001≤m≤100 ) — the number of the expedition participants and the number of the daily food packages available.

The second line contains sequence of integers a1,a2,…,ama1,a2,…,am (1≤ai≤1001≤ai≤100 ), where aiai is the type of ii -th food package.

Output

Print the single integer — the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.

Examples

Input

Copy

4 10
1 5 2 1 1 1 2 5 7 2

Output

Copy

2

Input

Copy

100 1
1

Output

Copy

0

Input

Copy

2 5
5 4 3 2 1

Output

Copy

1

Input

Copy

3 9
42 42 42 42 42 42 42 42 42

Output

Copy

3

Note

In the first example, Natasha can assign type 11 food to the first participant, the same type 11 to the second, type 55 to the third and type 22 to the fourth. In this case, the expedition can last for 22 days, since each participant can get two food packages of his food type (there will be used 44 packages of type 11 , two packages of type 22 and two packages of type 55 ).

In the second example, there are 100100 participants and only 11 food package. In this case, the expedition can't last even 11 day.

这道题就是给你n个人,每个人只能分配同一个数,求分成n个块,每个块内的数字相同,且每个块长度相同,求最小满足的长度。我的思想是,相同数字放一块,求每个数字有多少个,然后先分给n个人,块数>=n很好做,分完之后第一块/2,判断和最小块哪个大,如果小的话就结束这一,如果大的话就下一个/2再比较,除完一遍之后再/3,总之就是从大的地方越分越多。把最小块越来越往前提,但小于n,蒙了,需要判断啥时候满足条件再除。贪心+模拟的思想。

这样写真的是有点难。

还有一种由于数据比较小,我们直接枚举答案,判断答案对不对就可以了。从100-1枚举得到一个答案,从最长块开始分,分到最后如果能分到n个就是答案,因为倒着枚举第一个答案自然是最大的。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>

#define MOD 10000000
using namespace std; 
typedef long long LL;
typedef unsigned long long ULL;
ULL base=233;
void read(int &x)//'&'表示引用,也就是说x是一个实参,在函数中改变了x的值就意味着在外面x的值也会被改变
{
    int f=1;//标记正负
    x=0;//归零(这就是潜在bug,有可能传进来时x没有归零)
    char s=getchar();//读入第一个字符
    while(s<'0'||s>'9')//不是数字字符
    {
        if(s=='-')//不能直接把f=-1,有可能输入的不是'-'而是其他乱七八糟的东西
            f=-1;
        s=getchar();//继续读
    }
    while(s>='0'&&s<='9')//是字符(一旦不是字符就意味着输入结束了)
    {
        x=x*10+s-'0';
        s=getchar();
    }
    x*=f;//改变正负
}
long long quickpow(long long n, long long base) {
    long long res = 1;
    while(n) {
        if(n & 1) {
            res = res * base % MOD;
        }
        n >>= 1;
        base = base * base % MOD;
    }
    return res;
}//快速幂 
ULL HASH(char s[])
{
	int l=strlen(s);
	ULL ans=0;
	for(int i=0;i<l;i++)
	{
		ans=ans*base+(ULL)s[i];
	}
	return ans;
}
set<ULL>hash0;//手动哈希 
set<ULL>::iterator it;//STL迭代器 

set<int >set0;
char a;
int A[500];
int len[500];
int main()
{
       int n,m;
       cin>>n>>m;
       for(int i=1;i<=m;i++)
       {
       	 cin>>A[i];
       }
       sort(A+1,A+1+m);
       memset(len,0,sizeof(len));
       len[1]=1;
       int k=1;
       for(int i=2;i<=m;i++)
       {
       	  if(A[i]==A[i-1])
       	  {
  	       	len[k]++;
  	       }
  	       else
  	       {
       	  	k++;
       	  	len[k]=1;
       	  }
       }
    //   cout<<endl;
       sort(len+1,len+1+k);
   //    for(int i=1;i<=k;i++)
     //  cout<<len[i]<<" ";
      // cout<<endl;
       int flag;
       int ans;
       for(int i=m;i>=1;i--)
       {
       	
       	   flag=0;
       	   if(i*n<=m)
       	   {
      // 	   	cout<<i<<":";
                for(int j=k;j>=1;j--)
                {
                	ans=len[j];
                	while(1)
                	{
             		    
	                	if(ans>=i)
	                	{flag++;
	                	ans-=i;
						}
						if(flag==n)break;
	                	if(ans<i)break;
	                }
					
	         //       cout<<flag<<endl;
	                if(!flag)break;
	                if(flag==n)break;
                }
                if(flag==n)
                {
                	cout<<i<<endl;
                	return 0;
                }
   	       }
       }
       cout<<"0"<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值