2018QBXT刷题游记(1)

【2018QBXT刷题游记】

Day1 TEST1
T1

【题目大意】输入n,求n与 246913578 246913578 246913578的最小公倍数,对1234567890取模

对于 30%的数据, n &lt; = 1 0 9 n&lt;=10^{9} n<=109
对于 60%的数据, n &lt; = 1 0 18 n&lt;=10^{18} n<=1018
对于 100%的数据, n &lt; = 1 0 100000 n&lt;=10^{100000} n<=10100000

【慌乱分析】这是个什么鬼数??又是什么鬼数据范围???
当我敲出const int qaq=246913578;时,好像突然意识到了什么?1到9各出现了一次啊,一定有蹊跷!于是求了一下它的因数:
在这里插入图片描述

妙啊!123456789!
也就是说它是1234567890的1/5,所以只用求输入的n%5余数就好啦(考场想法,70分w)

于是得到了70分的…好成绩…(QAQ)
下面是冗长丑陋的代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
#define ll long long
ll qwq[35],qaq[35],f1[35],f2[35];
void init(){
	qwq[1]=2;qwq[2]=3;qwq[3]=6;qwq[4]=9;qwq[5]=18;
	qwq[6]=3607;qwq[7]=3803;qwq[8]=7214;qwq[9]=7606;
	qwq[10]=10821;qwq[11]=11409;qwq[12]=21642;
	qwq[13]=22818;qwq[14]=32463;
	qwq[15]=34227;qwq[16]=64926;qwq[17]=68454;
	qwq[18]=13717421;qwq[19]=27434842;
	qwq[20]=41152263;qwq[21]=82304526;
	qwq[22]=123456789;qwq[23]=246913578;
	for(int i=1;i<=22;i++)qaq[i]=qwq[23-i];
	qaq[23]=1;
}
ll n;
int emm=246913578;
#define MOD 12345678
int main(){
	freopen("lcm.in","r",stdin);
	freopen("lcm.out","w",stdout);
	init();
	cin>>n;
	if(n<emm){
		ll tmp=0;ll q;
		for(int i=22;i>=1;i--){
			if(n%qwq[i]==0){
				tmp=n/qwq[i];
				q=qwq[i];
				break;
			}
		}
		if(tmp){
			int f=tmp%5;
			cout<<emm*f<<endl;
			return 0;
		}
		else{
			int f=n%5;
			cout<<emm*f<<endl;
			return 0;
		}
	}
	if(n==emm){
		cout<<emm<<endl;
			return 0;
	}
	if(n>emm){
		ll tmp=0;ll q;
		for(int i=22;i>=1;i--){
			if(n%qwq[i]==0){
				tmp=n/qwq[i];
				q=qwq[i];
				break;
			}
		}
		if(tmp){
			int f=tmp%5;
			cout<<emm*f<<endl;
			return 0;
		}
		else{
			int f=n%5;
			cout<<emm*f<<endl;
			return 0;
		}
	}	
	return 0;
}

当看到解析的时候我是忧伤的…
啊…要是推一推公式就100了…

输入高精度数a与普通整数b求lcm,对c取模,c是b的倍数“”

l c m ( x , y ) = x y g c d ( x , y ) lcm(x, y) = \frac{xy}{gcd(x,y)} lcm(x,y)=gcd(x,y)xy

g c d ( x , y ) = g c d ( x − y , y ) gcd(x, y)=gcd(x − y, y) gcd(x,y)=gcd(xy,y)

可知 l c m ( a , b ) = a b g c d ( a m o d &ThinSpace;&ThinSpace; b ,   b ) lcm(a, b) =\frac{ab}{gcd(a\mod b,\ b)} lcm(a,b)=gcd(amodb, b)ab

由于c是b的倍数 l c m ( a , b ) = a b g c d ( a m o d &ThinSpace;&ThinSpace; c ,   b ) lcm(a,b)=\frac{ab}{gcd(a\mod c,\ b)} lcm(a,b)=gcd(amodc, b)ab

$lcm(a,b)\equiv \frac {(a\ mod\ c)b}{gcd((a\mod c),b)}(mod \ c) $

所以可以边输入边取模,不涉及高精问题了!

修改版代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int MOD=1234567890;
const int qaq=246913578;
ll gcd(ll x,ll y){
	if(y==0)return x;
	else return gcd(y,x%y);
} 
int main(){
	char t;
	t=getchar();
	ll n;
	while(t>='0'&&t<='9'){
		n=(n*10+t-'0')%MOD;//边读入边取模,学到了
		t=getchar();
	}
	n=n/gcd(n,246913578);
	n=n*qaq%MOD;
	cout<<n<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值