您的位置:首页 > 编程语言

CDU集训代码:初等数论

2012-08-13 20:10 92 查看
做题地址:http://acm.hdu.edu.cn/diy/contest_login.php?cid=16649

Problem K: HDU 1061

//计算+规律,可以用纯规律

for(i=0; i<N%4-1; i++)  N%4是规律推出来的最小公倍数


#include <stdio.h>

int main()
{
int T, N, i, ans, tmp;
scanf("%d", &T");
while(T--)
{
scanf("%d", &N");
if(N>10)
tmp = N%10;
else
tmp = N;
ans = tmp;
for(i=0; i<N%4-1; i++)
{
ans *=tmp;
if(ans>10)
ans %= 10;
}
printf("%d\n", ans);
}
return 0;
}


纯规律做这道题

#include <stdio.h>

int main ()
{
int t;
scanf ("%d", &t");
while ( t -- )
{
int n, a;
scanf ("%d", &n");
a = n % 10;
if ( a == 0 )
puts("0");
else if ( a == 1 )
puts("1");
else if ( a == 5 )
puts("5");
else if ( a == 6 )
puts("6");
else if ( a == 2 )
{
if ( n % 4 == 0 )
puts("6");
else if ( n % 4 == 1 )
puts("2");
else if ( n % 4 == 2 )
puts("4");
else if ( n % 4 == 3 )
puts("8");
}
else if ( a == 3 )
{
if ( n % 4 == 0 )
puts("1");
else if ( n % 4 == 1 )
puts("3");
else if ( n % 4 == 2 )
puts("9");
else if ( n % 4 == 3 )
puts("7");
}

else if ( a == 4 )
{
if ( n % 2 == 0 )
puts("6");
else if ( n % 2 == 1 )
puts("4");
}

else if ( a == 7 )
{
if ( n % 4 == 0 )
puts("1");
else if ( n % 4 == 1 )
puts("7");
else if ( n % 4 == 2 )
puts("9");
else if ( n % 4 == 3 )
puts("3");
}

else if ( a == 8 )
{
if ( n % 4 == 0 )
puts("6");
else if ( n % 4 == 1 )
puts("8");
else if ( n % 4 == 2 )
puts("4");
else if ( n % 4 == 3 )
puts("2");
}

else if ( a == 9 )
{
if ( n % 2 == 0 )
puts("1");
else if ( n % 2 == 1 )
puts("9");
}
}
return 0;
}


Problem H:HDU 2114

求1到N的立方和

#include <stdio.h>

int main()
{
__int64 sum,n;
while(scanf("%I64d",&n)!=EOF)
{
sum = 0;
if(n>10000)
n = n%10000;
sum = (n*(n+1)/2)*(n*(n+1)/2);
sum = sum%10000;
printf("%04I64d\n",sum);
}
return 0;
}


Problem C: HDU 1222

#include <stdio.h>

int main()
{
int t,m,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
while(m>n?(m%=n):(n%=m));
if(m+n==1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: